Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Anna Filina:
Testing Methods That Make Static Calls
Jan 13, 2016 @ 15:03:40

Anna Filina has posted a quick hint around testing methods that make static methods calls to other parts of your application. Static method calls are notoriously difficult to test, especially with PHPUnit.

I had trouble testing a particularly painful codebase. It had static calls and implicit dependencies all over the place, to name just a few problems.

One of the things that it often did was to call static methods that would increment counters in the database and cache stuff. Example: Record::incrementViews() It was making things difficult. To avoid messing with the original codebase too much, I came up with this quick and dirty way to ignore those dependencies.

Her solution makes use of a mockStaticDependency method that then turns around and redefines the class in question (like her "Record" above) with a __callStatic through an eval. She points out that usually using eval is "evil" but in this case it made testing the functionality much simpler when no feedback was needed from the static method. In the comments on the post, someone also makes a recommendation of the Patchwork library for PHP that allows for "monkey patching" and modifying classes/functionality to redefine functions and methods in a similar way.

tagged: unittest method static call monkeypatch eval callstatic example

Link: http://afilina.com/testing-methods-that-make-static-calls/

Andrew Podner:
Overloading: Create PHP Class Methods on the Fly
Mar 06, 2013 @ 17:51:57

Andrew Podner has a new post today looking at dynamic class method creation in PHP - aka "overloading" with the __call magic method.

What is overloading and what would I need it for? [...] In most languages, overloading just means you can have multiple methods with the same name, but they just had a different number/type of arguments. In PHP, it is a little different. Overloading in PHP means that you can actually create dynamic function names and the behavior will be dependent upon the function name that is used.

He gives an example through a sample application, first stating the requirements the business has for it then showing how to use the "__call" method to handle "getBy" requests made to a database class. It searches the database based on the field (ex. "getByusername" searches on "username") and he includes two examples of it in use. He also briefly touches on the use of the "__callStatic" magic method for handling static method calls similarly.

tagged: method overloading magicmethod call callstatic getby

Link:

Content With Style:
buggy behaviour of parent:: in PHP 5.3.3
Dec 20, 2010 @ 15:43:56

On the Content with Style blog today they take a look at some buggy functionality with parent:: they've found in the latest version of PHP, 5.3.3.

So, this app I hadn't been looking at in a few months did not work at all. I traced the bug down to a method that itself called a parent method. The parent only contains __call and __callStatic methods, and for some reason __callStatic was called, although the class it was called from was an object instance.

After using some sample code from the PHP manual, they spotted how things truly worked - the call is always sent to __callStatic when called from the extending class but not when called directly. Apparently they aren't the only ones that noticed the bug but noted that it was removed in PHP 5.3.4 to fix the issue.

tagged: bug version parent static callstatic method

Link:

Gergely Hodicska's Blog:
What is new in PHP 5.3 - Part 4
Nov 20, 2007 @ 15:38:00

Gergely Hodicska has posted part four of his "what's new in PHP 5.3" series - a sort of "wrapup" for some of the smaller features that have been added. Among them are:

  • __callStatic
  • OpenID support
  • user.ini user defined ini functionality
  • dynamic static calls
  • XSLT profiling

...and many more. Check out the post for more to add to the list and for some brief examples of the ones already mentioned.

tagged: callstatic openid xslt profiling userini new php5 callstatic openid xslt profiling userini new php5

Link:

Gergely Hodicska's Blog:
What is new in PHP 5.3 - Part 4
Nov 20, 2007 @ 15:38:00

Gergely Hodicska has posted part four of his "what's new in PHP 5.3" series - a sort of "wrapup" for some of the smaller features that have been added. Among them are:

  • __callStatic
  • OpenID support
  • user.ini user defined ini functionality
  • dynamic static calls
  • XSLT profiling

...and many more. Check out the post for more to add to the list and for some brief examples of the ones already mentioned.

tagged: callstatic openid xslt profiling userini new php5 callstatic openid xslt profiling userini new php5

Link:

Gergely Hodicska's Blog:
What's new in PHP 5.3 - part 1: namespaces
Nov 14, 2007 @ 18:50:00

Gergely Hodicska has started a mini-series of posts centering about what's going to be coming in the next major release of PHP, version 5.3. The first article has been posted today focusing on one of the more popular topics - namespaces.

In my previous post I mentioned that PHP 5.3 will be released in early 2008 so I think it's just in time to talk about the features of this version. [...] The big gun features are namespaces, late static binding and mysqlnd, but there are other interesting improvements, for example __callStatic, dynamic static calls. In this part of this series we are going to analyze namespaces in detail.

Included in the post are a "before" and "after" example of namspacing in a PHP application, code examples of namespaces in action, simple porting of current PHP 5.2 applications up to 5.3 and working with __autoload and Reflection.

tagged: namespace php5 callstatic latestaticbinding tutorial namespace php5 callstatic latestaticbinding tutorial

Link:

Gergely Hodicska's Blog:
What's new in PHP 5.3 - part 1: namespaces
Nov 14, 2007 @ 18:50:00

Gergely Hodicska has started a mini-series of posts centering about what's going to be coming in the next major release of PHP, version 5.3. The first article has been posted today focusing on one of the more popular topics - namespaces.

In my previous post I mentioned that PHP 5.3 will be released in early 2008 so I think it's just in time to talk about the features of this version. [...] The big gun features are namespaces, late static binding and mysqlnd, but there are other interesting improvements, for example __callStatic, dynamic static calls. In this part of this series we are going to analyze namespaces in detail.

Included in the post are a "before" and "after" example of namspacing in a PHP application, code examples of namespaces in action, simple porting of current PHP 5.2 applications up to 5.3 and working with __autoload and Reflection.

tagged: namespace php5 callstatic latestaticbinding tutorial namespace php5 callstatic latestaticbinding tutorial

Link:

Johannes Schlüter's Blog:
PHP 5.3 Update
Oct 13, 2007 @ 18:30:00

Johannes Schlüter, the new release manager for the PHP 5.3 series, has posted an update on the progress of the release including a brief list of the new things included in the update.

Since a few hours we're having, thanks to Edin, Windows snapshots for the upcoming PHP 5.3 release. In combination with the latest sources from CVS everybody should be able to test the current state.

Included in his list of updates are things like: namespaces, late static binding, the __callStatic magic method and the mysql native driver (mysqlnd). He also recommends that, if you're currently running something in the PHP 5.2.x series, you update to the latest on that before making the jump to PHP 5.3 when it comes out.

tagged: php5 update namespace callstatic latestaticbinding mysqlnd php5 update namespace callstatic latestaticbinding mysqlnd

Link:

Johannes Schlüter's Blog:
PHP 5.3 Update
Oct 13, 2007 @ 18:30:00

Johannes Schlüter, the new release manager for the PHP 5.3 series, has posted an update on the progress of the release including a brief list of the new things included in the update.

Since a few hours we're having, thanks to Edin, Windows snapshots for the upcoming PHP 5.3 release. In combination with the latest sources from CVS everybody should be able to test the current state.

Included in his list of updates are things like: namespaces, late static binding, the __callStatic magic method and the mysql native driver (mysqlnd). He also recommends that, if you're currently running something in the PHP 5.2.x series, you update to the latest on that before making the jump to PHP 5.3 when it comes out.

tagged: php5 update namespace callstatic latestaticbinding mysqlnd php5 update namespace callstatic latestaticbinding mysqlnd

Link:


Trending Topics: