NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP Undocumented Functionality...

Dan Cech dcech at phpwerx.net
Tue May 31 11:43:45 EDT 2005


Flavio,

This seems to me like a side effect of the mechanism used to allow 
directly calling methods defined in a superclass from a subclass.

eg:

class mysuper
{
   var $name = 'mysuper';
   function hello()
   {
     echo 'hello, I am '. $this->name;
   }
}

class mysub extends mysuper
{
   var $name = 'mysub';
   function hello()
   {
     parent::hello();
     echo ' testing';
   }
}

$mysub =& new mysub();
$mysub->hello();

The $this reference in mysuper::hello() should reference the $mysub 
object, which it does.  I would guess that the behavior you're seeing is 
that same mechanism in effect.

Dan

Flavio daCosta wrote:
> 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"
> ?>
> 
> 
> Thanks,
> Flavio
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org




More information about the talk mailing list