note 85230 rejected from language.oop5.paamayim-nekudotayim by mgf

From: Date: Wed, 27 Aug 2008 11:59:02 +0000
Subject: note 85230 rejected from language.oop5.paamayim-nekudotayim by mgf
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-143866@lists.php.net to get a copy of this message
Note Submitter: haprog at haprogszone dot net ---- Here's a problem I ran into. (on PHP 5.2.0) I'm not sure if this is a bug or not but here is an example that gives unexpected results in my opinion: <?php header('Content-type: text/plain'); abstract class SQLTable { public static $tablename; public static $columns; public static function getData($where) { // debug var_dump(self::$tablename); var_dump(self::$columns); // implement return data as array from SQL query: // "SELECT ".implode(', ', self::$columns)." FROM ".self::$tablename." WHERE ".$where } } class MyTable extends SQLTable { public static $tablename = 'mytable'; public static $columns = array( 'id', 'text', ); } $table = new MyTable(); $table->getData('id=1'); // Call #1 MyTable::getData('id=1'); // Call #2 ?> I would expect both of these calls to echo: string(7) "mytable" array(2) { [0]=> string(2) "id" [1]=> string(4) "text" } Instead I get: NULL NULL If I redeclare the function getData in MyTable exactly as it is now declared in SQLTable, then the example will work as expected. Do I really need to repeat this exact declaration in every class that extends SQLTable to get this functionality? I could also make it work when I removed all static keywords and replaced all "self::$var" with "$this->var" but then they aren't static and Call #2 results in a fatal error. Is self:: supposed to work like this when extending classes? If so, then I see a need for a similar keyword for example this:: that would use the values from the extending class.

« previous php.notes (#143866) next »