Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

2016-05-19

debug_backtrace and call_user_func in PHP 7

Before PHP 7, functions call_user_func() and call_user_func_array() appeared in debug_backtrace() as separate entries. Since the version 7, they do not.

Demonstration code Gist

2015-08-12

PHPStorm: mark a single file as excluded

PHPStorm allows to mark a folder as "Excluded" (Go to Project view, right-click on a folder and choose "Mark Directory as... Excluded". Then, all the files in that folder will be ignored by the PHPStorm when it looks for functions, constants, etc. So, for example, if you have an older versions of a library, in "archive" folder, you can exclude it, and Code Inspection will not refer to those older method definitions, constants, etc.

But what if you need to exclude just a single file? The only way I found was to mark it as a plain text:



It's particularly useful for WordPress projects: those two files

wp-admin/load-scripts.php
wp-admin/load-styles.php

have tons of "dummy" methods:

/**
* @ignore
*/
function add_filter() {}

- with no parameters or bodies. PHPStorm finds them, considers no params and a void return and issues useless inspection errors.

UPDATE:
There is an alternative solution, suggested by Slava Abakumov: add the file(s) to the Settings->Editor->File Types->Ignore...
The drawback is that the file disappears completely from the Project view, and even by typing its name, you won't be able to find it. Ignored completely. This can be quite inconvenient if you want to ignore a file, but still be able to edit it.




--------
A Pro-tip from the creators of WPGlobus Multilingual Plugin for WordPress

2015-02-19

Codeception: XPath `...` is malformed!

Environment:

Codeception PHP Testing Framework v2.0.11
Powered by PHPUnit 4.4.5 by Sebastian Bergmann.

Symptom:

* I am on page "/"
* I click "Terms of Use"

  XPath `Terms of Use` is malformed!

Cause:

$I->click( "Terms of Use" );

Cure:

$I->click(['link' => "Terms of Use"]);

From the documentation:

http://codeception.com/docs/modules/WebDriver#click

// using strict locator
$I->click(['link' => 'Login']);

2014-01-04

Could not open input file: /cygdrive/c/PHP/phpunit (PHP under Cygwin)

The problem:

$ /cygdrive/c/PHP/php.exe /cygdrive/c/PHP/phpunit
Could not open input file: /cygdrive/c/PHP/phpunit

Environment:

MS Windows
PHP
Cygwin

Explanation:

While started in Cygwin, php.exe will be run in Windows environment, which does not know how to translate /cygdrive/c to C:\

Solution:

Use a wrapper script https://gist.github.com/tivnet/8256140
(Originally published by aefxx here)

2013-06-19

WordPress: remove admin menu items

There are several well-written WordPress plugins that allow removing and re-ordering admin menu items. However, if you want something quick and simple for your site, the code snippet below may be useful. It removes all menus, except for Dashboard and WooCommerce "Products".

Feel free to modify is as necessary.

► Tip: you can print_r($menu) to see what to keep.


add_action('admin_menu', function () {
 if (current_user_can('administrator')) {
  return;
 }

 /**
  * Keep only specific menu items and remove all others
  */
 global $menu;
 $hMenu = $menu;
 foreach ($hMenu as $nMenuIndex => $hMenuItem) {
  if (in_array($hMenuItem[2], array(
       'index.php',
       'edit.php?post_type=product',
      ))
  ) {
   continue;
  }
  unset($menu[$nMenuIndex]);
 }
}

2012-11-27

2 Great Articles on Custom Post Types in WordPress

By Justin Tadlock:
http://justintadlock.com/archives/2010/04/29/custom-post-types-in-wordpress
In WordPress 3.0, we’ll have the capability to easily create and manage content via custom post types. Not only that, but you won’t have to rely on a plugin to do this for you. It can be done via your theme’s functions.php file with a few lines of code.
By Richard Shepherd:
http://blog.teamtreehouse.com/create-your-first-wordpress-custom-post-type
...I think the great thing about WordPress is we can use it in the way we feel it should be used. There might be a different or better way, but there is no wrong way to do something (unless it doesn’t work!).
► Both articles provide a great introduction to the custom post types, with detailed PHP code examples. Custom types allow to associate additional information (fields) with a specific group of WordPress posts, and distinguish those posts from others in the admin interface. Having them, it's possible to extend WordPress to a powerful dabatase application.

2012-10-13

Is WordPress good for building sites? A SEO Group discussion on LinkedIn


This is an excerpt from discussion on the Search Engine Land’s LinkedIn group:
http://lnkd.in/gigffm - copied almost as-is (fixed a couple of typos)
►LP: I found this article to be a good read:
http://biztwozero.com/Home/6181
► My reply
---------------
@LP: I am with you on this (PHP, etc.), and I already expressed my opinion about the quality of PHP code in WordPress and potential security risks - in other discussions (look for my recent posts).

However, do not take that specific article as a 100% truth. While correct in general, many specific details are not.

Why don't you just try for yourself? Installing WP takes literally 5 minutes, as long as you have Apache/MySQL/PHP at your disposal. MySQL can be anywhere, of course, and you can specify the remote host. Apache and PHP can be at your PC, so you can easily play with the themes, plugins and the core code - as a PHP programmer and not as a WP user. You will see everything.
The question is, however, what is your goal and what do you compare WP with? If you are building a "non-mission-critical" website, who cares that WP will throw PHP notices on every line of its lousy code? Disable all errors and sleep well :-)

If you compare to other open-source CMS/Blogging platforms - they will suffer more or less from the same "diseases".

If, however, you desperately need a solution for content editing, and you do not want to spend $ on a commercial - there are two ways, in my opinion: a) take WordPress, find a premium theme, which will provide you with all the functionality (that will cost $ instead of $$$) - and use ONLY that theme and only their plugins. Once you start mixing plugins from different vendors, one day you upgrade one, and it will bring down another, because they do not know each other, and write whatever they want, overriding the core functionality.

Or b) Because WP writes into MySQL table, you can use PHP to extract the content from there and publish it your way. That's similar to having a premium theme, but also allows you to put WP on a different server, solving thus some security problems (the WP can be installed even at your home, as I said)

There is a1) - write the Premium Theme yourself :-)

I tried the a) and a1) approaches since WP was born, and each time I ended up with version b) or with no WP at all....

2011-08-08

PHP on FreeBSD : Segmentation fault: 11 (core dumped)

PHP started dumping core after I upgraded MySQL to 5.5.
Everything worked fine, but a core was dumped every time, probably at the exit.

The solution was to rebuild PHP with the
"LINKTHR Link thread lib (for threaded extensions)"
option.

Later, I found a confirmation in this post

2010-11-23

PHP namespaces and autoload on UNIX

The namespace "path" symbol is backslash. Therefore, the full class name looks like:
MyNameSpace\MyClass. We need to replace the backslash with slash in the __autoload function, in order for it to work under UNIX. Under Windows, it works either way.

function __autoload( $class_name ) {
# ------------------------------------
$class_name = str_replace( '\\', '/', $class_name );
require_once $class_name . ".class.php";
}

2010-07-06

Upgrading to PHP 5.3 ports on FreeBSD

Environment:
FreeBSD

php5-5.2.12         PHP Scripting Language
php5-curl-5.2.12    The curl shared extension for php
php5-dom-5.2.12     The dom shared extension for php
php5-gd-5.2.12      The gd shared extension for php
php5-iconv-5.2.12   The iconv shared extension for php
php5-mbstring-5.2.12 The mbstring shared extension for php
php5-mysql-5.2.12   The mysql shared extension for php
php5-openssl-5.2.12 The openssl shared extension for php
php5-pcre-5.2.12    The pcre shared extension for php
php5-pdo-5.2.12     The pdo shared extension for php
php5-pdo_mysql-5.2.12 The pdo_mysql shared extension for php
php5-session-5.2.12 The session shared extension for php
php5-simplexml-5.2.12 The simplexml shared extension for php
php5-spl-5.2.12     The spl shared extension for php
php5-xml-5.2.12     The xml shared extension for php
php5-xsl-5.2.12     The xsl shared extension for php
php5-zip-5.2.12     The zip shared extension for php
php5-zlib-5.2.12    The zlib shared extension for php
pecl-stem-1.5.0     A PECL extension which provides word stemming

Task:
Upgrade all ports to php5-5.3.2

  1. Some of the functions are deprecated in PHP 5.3. I had to replace all occurrences of split() with explode() or preg_split() and mysql_escape_string() with mysql_real_escape_string(). Note that mysql_real_escape_string() requires second parameter - DB connection.
  2. Attempt to upgrade ports with `portupgrade php5*` did not work. Some of the extensions do not exist anymore in PHP 5.3, and `pkgdb` was quite confused. I had to delete all php5 ports and then install them again.
  3. PHP 5.3 installed extensions into a new folder /usr/local/lib/php/20090626. My php.ini had reference to the old one, /usr/local/lib/php/20060613. Had to change that manually.
  4. pecl-stem-1.5.0 was not marked for upgrade (pkg_version). However, I had to rebuild it, because it was incompatible with the new PHP libraries.
  5. `php -i` complained about undefined timezone. I had to add date.timezone="America/New_York" to php.ini.
  6. I checked which extensions are now "included with PHP by default" (in docs on http://php.net and in /usr/ports/lang/php5-extensions.Makefile), and installed some of them, in addition to what I had before. There was also a nice "fileinfo" extension that I thought could be useful. Here is the list of extensions after upgrade:
php5-5.3.2_1        PHP Scripting Language
php5-ctype-5.3.2_1  The ctype shared extension for php
php5-curl-5.3.2_1   The curl shared extension for php
php5-dom-5.3.2_1    The dom shared extension for php
php5-filter-5.3.2_1 The filter shared extension for php
php5-gd-5.3.2_1     The gd shared extension for php
php5-hash-5.3.2_1   The hash shared extension for php
php5-iconv-5.3.2_1  The iconv shared extension for php
php5-json-5.3.2_1   The json shared extension for php
php5-mbstring-5.3.2_1 The mbstring shared extension for php
php5-mysql-5.3.2_1  The mysql shared extension for php
php5-openssl-5.3.2_1 The openssl shared extension for php
php5-pdo-5.3.2_1    The pdo shared extension for php
php5-pdo_mysql-5.3.2_1 The pdo_mysql shared extension for php
php5-session-5.3.2_1 The session shared extension for php
php5-simplexml-5.3.2_1 The simplexml shared extension for php
php5-xml-5.3.2_1    The xml shared extension for php
php5-xmlreader-5.3.2_1 The xmlreader shared extension for php
php5-xmlrpc-5.3.2_1 The xmlrpc shared extension for php
php5-xmlwriter-5.3.2_1 The xmlwriter shared extension for php
php5-xsl-5.3.2_1    The xsl shared extension for php
php5-zip-5.3.2_1    The zip shared extension for php
php5-zlib-5.3.2_1   The zlib shared extension for php
pecl-fileinfo-1.0.4 A PECL extension to retrieve info about files
pecl-stem-1.5.0     A PECL extension which provides word stemming
 

2010-05-06

PDO bug: exception caught but reappears in foreach() loop

I was experimenting with PDO/MySQL trying to see how a PHP code will behave with a single static DB connection, multiple connections but declared as "persistent", unbuffered MySQL queries and so on - and discovered a bug which I'd like to show here, because it took me a while to find any reference to it.
I tested it with PHP 5.2.13 under both UNIX and Windows.
Below is the code that demonstrates the problem, and two solutions/workarounds:

#
# PDO foreach exception bug
# Demonstration code
# Author: http://www.tiv.net
# 2010-05-06
#
# Note: same results will appear if using "while fetch()"
# instead of "foreach()" loop
#

print 'This code works OK (Exception is cleaned artificially)
';
$conn = new PDO( 'mysql:host=localhost', 'test', 'test' );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$oRS = $conn->query( "select 'OK'" );
foreach ( $oRS as $row ) {
    try {
        $conn->query( 'Bogus SQL' );
    } catch (PDOException $e) {}
    if ( $conn->errorCode() !== '00000' ) {
        $conn->query( "select 'CLEAN_PDO_ERROR'" );
    }
print 'NO exception will be thrown.
';
}

print 'This code works OK (two separate connections)
';
$conn = new PDO( 'mysql:host=localhost', 'test', 'test' );
$conn2 = new PDO( 'mysql:host=localhost', 'test', 'test' );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$conn2->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$oRS = $conn->query( "select 'OK'" );
foreach ( $oRS as $row ) {
    try {
        $conn2->query( 'Bogus SQL' );
    } catch (PDOException $e) {}
print 'NO exception will be thrown.
';
}


print 'This code throws unexpected exception in foreach
';
$conn = new PDO( 'mysql:host=localhost', 'test', 'test' );
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$oRS = $conn->query( "select 'OK'" );
foreach ( $oRS as $row ) {
    try {
        $conn->query( 'Bogus SQL' );
    } catch (PDOException $e) {}
print 'Exception will be thrown after this...
';
}
?>