Workaround for inability to know calling class inside an inherited method?
| From: | Juan Alonso | Date: | Mon, 26 Apr 2004 16:34:49 +0000 |
| Subject: | Workaround for inability to know calling class inside an inherited method? | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-9568@lists.php.net to get a copy of this message | ||
If a class inherits a method from a parent function and you call the parent
method from the inherited class you cannot refer to variables referred in
the inherited class. Is there a language construct to refer to the calling
class inside a method?
The example will explain it better:
<?php
class db
{
static function query()
{
echo self::$connString;
}
}
class dbOne extends db
{
protected $connString = 'host=localhost blablla';
}
echo dbOne::query();
?>
And it generates:
Fatal error: Access to undeclared static property: db::$connString in
/var/dev/jalonso.gea/app/db_new.php on line 6
I know this has been brought before but I don't remember if there is any
workaround besides redefining the method in the inherited class and passing
the parent class it's name, which is what I am doing currently:
<?php
class db
{
static function query($connString)
{
echo $connString;
}
}
class dbOne extends db
{
protected $connString = 'host=localhost blablla';
static function query()
{
return parent::query(self::$connString);
}
}
echo dbOne::query();
?>
Regards,
Juan Alonso