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

Mike Naberezny's Blog:
__get() - an alternative to __autoload()
Jan 09, 2008 @ 12:47:54

Mike Naberezny has posted this new item on his blog today with a look at why __get() is a perfectly good alternative to __autoload() in a class structure.

__autoload() is a magic function introduced in PHP 5 that provides a mechanism for on-demand loading of classes. After its inclusion in PHP, many argued that using such a feature is too magical or not a good design practice. Putting the religious debates over the appropriateness of __autoload() aside, its implementation does have one significant drawback: it is a function declared in the global scope. Once a function is declared, it cannot be redeclared. This means __autoload() can't be used effectively in shared libraries, since any other code could have already declared it.

Similar lazy-load functionality can be achieved on the class level by using __get().

He gives a short code example where the __get() call mimics the functionality of __autoload(), but the resulting object created is public, not global...

tagged: __get __autoload alternative global versus public __get __autoload alternative global versus public

Link:

Mike Naberezny's Blog:
__get() - an alternative to __autoload()
Jan 09, 2008 @ 12:47:54

Mike Naberezny has posted this new item on his blog today with a look at why __get() is a perfectly good alternative to __autoload() in a class structure.

__autoload() is a magic function introduced in PHP 5 that provides a mechanism for on-demand loading of classes. After its inclusion in PHP, many argued that using such a feature is too magical or not a good design practice. Putting the religious debates over the appropriateness of __autoload() aside, its implementation does have one significant drawback: it is a function declared in the global scope. Once a function is declared, it cannot be redeclared. This means __autoload() can't be used effectively in shared libraries, since any other code could have already declared it.

Similar lazy-load functionality can be achieved on the class level by using __get().

He gives a short code example where the __get() call mimics the functionality of __autoload(), but the resulting object created is public, not global...

tagged: __get __autoload alternative global versus public __get __autoload alternative global versus public

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:

Greg Beaver's Blog:
phpDocumentor and __get/__set/__call - give us your ideas (RFC)
Jul 14, 2006 @ 11:06:00

In his latest post today, Greg Beaver is also taking a look at phpDocumentor and some of the documentation methods it allows, noting that providing the right notes on the "magic" functions has always been a point of difficulty.

One of the trickier feature requests for phpDocumentor has been documenting "magic" object properties and methods. By "magic" I am referring to properties and methods that are created dynamically by PHP 5.0+ userspace class methods __get, __set, __isset, __unset and __call.

He gives a code example of creating properties and a magic function (borp). To illustrate his point, he tries to specify the phpDocumentor format that would go with it - not an exact match, but with the help o ffour new tags it's made easier: @property, @property-read, @property-write, and @method.

tagged: phpdocumentor __get __set __call ideas magic functions property phpdocumentor __get __set __call ideas magic functions property

Link:

Greg Beaver's Blog:
phpDocumentor and __get/__set/__call - give us your ideas (RFC)
Jul 14, 2006 @ 11:06:00

In his latest post today, Greg Beaver is also taking a look at phpDocumentor and some of the documentation methods it allows, noting that providing the right notes on the "magic" functions has always been a point of difficulty.

One of the trickier feature requests for phpDocumentor has been documenting "magic" object properties and methods. By "magic" I am referring to properties and methods that are created dynamically by PHP 5.0+ userspace class methods __get, __set, __isset, __unset and __call.

He gives a code example of creating properties and a magic function (borp). To illustrate his point, he tries to specify the phpDocumentor format that would go with it - not an exact match, but with the help o ffour new tags it's made easier: @property, @property-read, @property-write, and @method.

tagged: phpdocumentor __get __set __call ideas magic functions property phpdocumentor __get __set __call ideas magic functions property

Link:

DevShed:
Implementing Property Overloading in PHP 4
Jul 11, 2006 @ 14:13:31

There's been a lot of fuss about the new object model in PHP5 lately, but what's a developer to do when he's stuck back in PHP4 and has no control over when things are updated? Do you just miss out on some of those cool features? Well, you may not have access to what PHP5-ers do, but PHP4 still has some cool tricks up its sleeve. One of which is property overloading, and it's covered in this new article from DevShed.

In these articles, I'll explain the basics of class overloading, starting with the application of the "overload()" function in PHP 4, in conjunction with using the "_set()", "__get()" and "__call()" methods, accompanied of several practical examples, so you’ll have a clear idea of how to overload your classes. Also, I'll cover class overloading in PHP 5, which offers native support for overloading methods and properties through the built-in methods that I mentioned before.

This first part of the series lays down the groundwork of overloading, touching briefly on its uses before moving onto some of the functionality - the __set method, overload function, and __get method - to make a simple "cookie saver" application.

tagged: overload php4 php5 property __set __get tutorial overload php4 php5 property __set __get tutorial

Link:

DevShed:
Implementing Property Overloading in PHP 4
Jul 11, 2006 @ 14:13:31

There's been a lot of fuss about the new object model in PHP5 lately, but what's a developer to do when he's stuck back in PHP4 and has no control over when things are updated? Do you just miss out on some of those cool features? Well, you may not have access to what PHP5-ers do, but PHP4 still has some cool tricks up its sleeve. One of which is property overloading, and it's covered in this new article from DevShed.

In these articles, I'll explain the basics of class overloading, starting with the application of the "overload()" function in PHP 4, in conjunction with using the "_set()", "__get()" and "__call()" methods, accompanied of several practical examples, so you’ll have a clear idea of how to overload your classes. Also, I'll cover class overloading in PHP 5, which offers native support for overloading methods and properties through the built-in methods that I mentioned before.

This first part of the series lays down the groundwork of overloading, touching briefly on its uses before moving onto some of the functionality - the __set method, overload function, and __get method - to make a simple "cookie saver" application.

tagged: overload php4 php5 property __set __get tutorial overload php4 php5 property __set __get tutorial

Link:

Sitening.com:
Getting Real With Databases in PHP
Jun 01, 2006 @ 11:11:58

This new post on the Sitening.com blog (written up by Tyler Hall) today takes a look at PHP and databases (MySQL specifically in this case) and "getting real" with them.

many of the ideas behind Ruby on Rails are spot on. I particularly like how well it abstracts database queries into separate objects for each table. That’s very cool and can save having to write a lot of repetitive code. But having to run a Rails script to generate new files is a little too cumbersome for me. Like I said, I prefer to keep things simple. Can we do the same thing with PHP? Using just one file?

Yes.

He proceeds to show a class that can perform operations similar to those styled in RoR - creating the object, applying proterties to it and performing the action (inserts/delete/etc). It's all made possible through the use of the __get and __set functions offered in PHP5. From there, it's just a simple matter of defining the functions for the actions.

To bring the point home, he also includes some examples of how to use this kind of functionality with a bit more than the previous example - selecting, updating, deleting, and inserting a new record (including the action function for each). Lastly, he gives an example of extending this base class, as mentioned in his first example (creating an object like "Animal" or "User" to abstract out the connections even more.

tagged: getting real databases rubyonrails class __get __set getting real databases rubyonrails class __get __set

Link:

Sitening.com:
Getting Real With Databases in PHP
Jun 01, 2006 @ 11:11:58

This new post on the Sitening.com blog (written up by Tyler Hall) today takes a look at PHP and databases (MySQL specifically in this case) and "getting real" with them.

many of the ideas behind Ruby on Rails are spot on. I particularly like how well it abstracts database queries into separate objects for each table. That’s very cool and can save having to write a lot of repetitive code. But having to run a Rails script to generate new files is a little too cumbersome for me. Like I said, I prefer to keep things simple. Can we do the same thing with PHP? Using just one file?

Yes.

He proceeds to show a class that can perform operations similar to those styled in RoR - creating the object, applying proterties to it and performing the action (inserts/delete/etc). It's all made possible through the use of the __get and __set functions offered in PHP5. From there, it's just a simple matter of defining the functions for the actions.

To bring the point home, he also includes some examples of how to use this kind of functionality with a bit more than the previous example - selecting, updating, deleting, and inserting a new record (including the action function for each). Lastly, he gives an example of extending this base class, as mentioned in his first example (creating an object like "Animal" or "User" to abstract out the connections even more.

tagged: getting real databases rubyonrails class __get __set getting real databases rubyonrails class __get __set

Link:


Trending Topics: