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

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:

DevShed:
Overloading and Object-Oriented Programming with PHP 5
Oct 12, 2006 @ 21:03:13

Continuing on with their series pulling from the great book from George Schlossnagle, "Advanced PHP Programming", DevShed has posted this new tutorial looking at the use of overloading with object oriented programming in your PHP 5 scripting expeditions.

Last week, we discussed design patterns and polymorphism. This week, we examine overloading and more. Let's bring together some of the techniques developed so far in this chapter and use overloading to provide a more OO-style interface to the result set. Having all the results in a single object may be a familiar paradigm to programmers who are used to using Java's JDBC database connectivity layer.

The first page shows (mostly in code) how to grab the information from the database and pull it all into the single, tidy result set. They dump this new data packet into the hands of the SPL and iterate through it, pulling out the key/value pairs along the way.

Finally, they look at two of the special functions PHP has to make life a little easier when working with objects - __call and __autoload. Both provide some of that great "automagical" functionality that makes life better.

tagged: object oriented book excerpt advanced overloading php5 object oriented book excerpt advanced overloading php5

Link:

DevShed:
Overloading and Object-Oriented Programming with PHP 5
Oct 12, 2006 @ 21:03:13

Continuing on with their series pulling from the great book from George Schlossnagle, "Advanced PHP Programming", DevShed has posted this new tutorial looking at the use of overloading with object oriented programming in your PHP 5 scripting expeditions.

Last week, we discussed design patterns and polymorphism. This week, we examine overloading and more. Let's bring together some of the techniques developed so far in this chapter and use overloading to provide a more OO-style interface to the result set. Having all the results in a single object may be a familiar paradigm to programmers who are used to using Java's JDBC database connectivity layer.

The first page shows (mostly in code) how to grab the information from the database and pull it all into the single, tidy result set. They dump this new data packet into the hands of the SPL and iterate through it, pulling out the key/value pairs along the way.

Finally, they look at two of the special functions PHP has to make life a little easier when working with objects - __call and __autoload. Both provide some of that great "automagical" functionality that makes life better.

tagged: object oriented book excerpt advanced overloading php5 object oriented book excerpt advanced overloading php5

Link:

DevShed:
Overloading Classes in PHP 5
Jul 26, 2006 @ 10:53:51

DevShed is wrapping up their "Overloading in PHP" series today with this last part - "Overloading Classes in PHP5".

It's time to move on and continue exploring the subject, since there are still a few additional topics that need to be covered. So, what's comes next now? All right, in this last part of the series, I'll be explaining how to overload classes specifically in PHP 5, which offers native support for overloading class members and methods, without having to explicitly call the PHP built-in "overload()" function.

They look first at using the __get method to replace __set in their previous code to save data out to a file. Then, to illustrate even more enhanced functionality, they implement __get and __set at the same time in a class, using them to grab values and set values to properties. Finally, they throw __call into the mix, showing how to overload method calls too.

tagged: overloading part3 tutorial classes php5 get set call overloading part3 tutorial classes php5 get set call

Link:

DevShed:
Overloading Classes in PHP 5
Jul 26, 2006 @ 10:53:51

DevShed is wrapping up their "Overloading in PHP" series today with this last part - "Overloading Classes in PHP5".

It's time to move on and continue exploring the subject, since there are still a few additional topics that need to be covered. So, what's comes next now? All right, in this last part of the series, I'll be explaining how to overload classes specifically in PHP 5, which offers native support for overloading class members and methods, without having to explicitly call the PHP built-in "overload()" function.

They look first at using the __get method to replace __set in their previous code to save data out to a file. Then, to illustrate even more enhanced functionality, they implement __get and __set at the same time in a class, using them to grab values and set values to properties. Finally, they throw __call into the mix, showing how to overload method calls too.

tagged: overloading part3 tutorial classes php5 get set call overloading part3 tutorial classes php5 get set call

Link:

DevShed:
Using Method Call Overloading in PHP 4
Jul 18, 2006 @ 17:44:36

DevShed has posted part two in the series they've worked up covering overloading classes inside of a PHP4 environment. In this edition (two of three) they focus on method call overloading, a step up from the previous property overloading.

This is part two of the series "Overloading classes in PHP." In three tutorials, this series teaches how to overload your classes in PHP 4 by using the "overload()" PHP built-in function, in conjunction with the implementation of the "__set()", "__get()" and "__call()" methods, and explores the native support of object overloading in PHP 5.

They first take a step back and go over the concepts behind the property overloading from the previous part of the series. After being reminded of that (or hearing it for the first time for some), they translate that directly into a method for use on methods. And, with the help of a __call request, they show you how to make it all work together and overload the method call.

tagged: method overloading php4 tutorial part2 __call __get __set method overloading php4 tutorial part2 __call __get __set

Link:

DevShed:
Using Method Call Overloading in PHP 4
Jul 18, 2006 @ 17:44:36

DevShed has posted part two in the series they've worked up covering overloading classes inside of a PHP4 environment. In this edition (two of three) they focus on method call overloading, a step up from the previous property overloading.

This is part two of the series "Overloading classes in PHP." In three tutorials, this series teaches how to overload your classes in PHP 4 by using the "overload()" PHP built-in function, in conjunction with the implementation of the "__set()", "__get()" and "__call()" methods, and explores the native support of object overloading in PHP 5.

They first take a step back and go over the concepts behind the property overloading from the previous part of the series. After being reminded of that (or hearing it for the first time for some), they translate that directly into a method for use on methods. And, with the help of a __call request, they show you how to make it all work together and overload the method call.

tagged: method overloading php4 tutorial part2 __call __get __set method overloading php4 tutorial part2 __call __get __set

Link:

Lukas Smith's Blog:
To serialize or to not serialize?
Jun 12, 2006 @ 11:01:46

In his latest post, Lukas Smith talks about serializing data - some of the pros and cons about it, as well as his experiences with it in the development of his own framework.

In my own framework I have decided that there is plenty of structured data that I will never query on that I will just stick into the database as a serialized array. Now the other day I modified the auto type handling in MDB2's quote() method to automatically serialize array's if no type is explicitly passed to the quote() method.

I send out a question to pear-dev@ and it was not unanimously well received. So I sat down and pondered a more elegant approach. Actually there is already one approach implemented in MDB2 since ages.

This implementation uses a seperate module that abuses PHP5's overloading functionality to integrate it easily. He also looked into another solution, however - creating a "datatype map" to help with the custom automatic serialization of datatypes (not just dependant on the variable type like before). Be sure to check out the comments for more thoughts on the subject, including validation of the custom datatypes.

tagged: serialize unserialize custom datatypes overloading modle mdb2 serialize unserialize custom datatypes overloading modle mdb2

Link:

Lukas Smith's Blog:
To serialize or to not serialize?
Jun 12, 2006 @ 11:01:46

In his latest post, Lukas Smith talks about serializing data - some of the pros and cons about it, as well as his experiences with it in the development of his own framework.

In my own framework I have decided that there is plenty of structured data that I will never query on that I will just stick into the database as a serialized array. Now the other day I modified the auto type handling in MDB2's quote() method to automatically serialize array's if no type is explicitly passed to the quote() method.

I send out a question to pear-dev@ and it was not unanimously well received. So I sat down and pondered a more elegant approach. Actually there is already one approach implemented in MDB2 since ages.

This implementation uses a seperate module that abuses PHP5's overloading functionality to integrate it easily. He also looked into another solution, however - creating a "datatype map" to help with the custom automatic serialization of datatypes (not just dependant on the variable type like before). Be sure to check out the comments for more thoughts on the subject, including validation of the custom datatypes.

tagged: serialize unserialize custom datatypes overloading modle mdb2 serialize unserialize custom datatypes overloading modle mdb2

Link:

Mike Naberezny's Blog:
pecl/operator and Other Neat Stuff
Feb 02, 2006 @ 12:42:59

Mike Naberezny has this new post on his blog today about the latest extension submitted by Sara Golemon to the PECL library - the pecl/operator extension.

This extension adds operator overloading support to PHP 5. I don’t think operator overloading fits the “PHP spirit” and as such I speculate it probably won’t ever make it into the core. Regardless, it’s interesting that this extension is now available and certainly makes for some fun experiments, especially if you’re already familiar with techniques from languages like C++.

He gives some sample code, and mentions the "magic methods" the extension allows. Later in the post, he also looks at a related package (also by Sara) that allows for self-modifying code in PHP - pecl/runkit.

tagged: pecl/operator magic methods operator overloading pecl/operator magic methods operator overloading

Link:


Trending Topics: