NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP Undocumented Functionality...

Hans Zaunere lists at zaunere.com
Tue May 31 12:02:13 EDT 2005


> Reading some interesting articles on http://phppatterns.com/ I came
> across the following some code that has me scratching my head...
> 
> Can anyone explain why the code below works!? (in php4 and php5)  And is
> this simply an undocumented _side effect_, or is it intentional (from
> the php developers)?
> 
> 1) Per the php manual, you can't use $this in static functions.
> 2) How is the SomeClass property $errorMsg accessed within the Debug class?
> 
> Example code from
> <http://www.phppatterns.com/index.php/article/articleview/15/1/1/>
> 
> 
> <?php
> class Debug {
>     function display () {
>         echo ($this->errorMsg);
>     }
> }
> 
> class SomeClass {
>     var $errorMsg='This is an error message';
>     function someFunction () {
>         if ( DEBUG == 1 ) {
>             Debug::display();
>         }
>     }
> }
> 
> define ('DEBUG',1);
> $someClass= &new SomeClass;
> $someClass->someFunction(); // Outputs: "This is an error message"
> ?>

Although I suppose it is officially undocumented, it's been fairly well known.  Regardless of whether people think it's right or not, it was left in PHP 5 because quite a bit of OO code depends on it.

While probably not good OO practice, I liked the functionality.  Although I don't use it anymore, I used to make extensive use of it as a multi-inheritance mechanism.  Pretty handy, although for the wrong reasons :)

H





More information about the talk mailing list