[PHP] Re: class problem :(
Luke wrote: Right I've read the manual on this and all that so hopefully you find people can help. I have an abstract class with three children. The abstract is ForumObject and the three children are Thread, Category and Post and each have their own table so I wrote the following: abstract class ForumObject { static private $table; static function getObjectIds ($field, $value) { $query = SELECT id FROM {self::$table} WHERE $field = '$value'; $object_ids = mysql_fetch_array(); return $object_ids; } } class Category extends ForumObject { static private $table = categories; } That's just got the important bits for the sake of your eyes but basically the problem I'm having is calling Category::getObjectIds ($whatever, $whatever2); Seems to think that it's referring to ForumObject::$table rather than Category::$table? I looked into it and there seems to be something you can do with get_called_class() but unfortunately I'm stuck with 5.2.9 at the moment and that is new to 5.3. Any ideas? Perhaps there is a different way I could implement the classes - I would rather not have getObjectIds repeated three times! Thanks in advance, I didn't test this, just a thought. You'd still have to implement getObjectIds(), but it would be slim: abstract class ForumObject { static private $table; static function getObjectIds ($field, $value, $class) { $query = SELECT id FROM {$class::$table} WHERE $field = '$value'; $object_ids = mysql_fetch_array(); return $object_ids; } } class Category extends ForumObject { static private $table = 'categories'; static function getObjectIds ($field, $value) { parent::getObjectIds ($field, $value, __CLASS__) } } Or probably the same because you're defining $table anyway in the child class: abstract class ForumObject { static private $table; static function getObjectIds ($field, $value, $table) { $query = SELECT id FROM $table WHERE $field = '$value'; $object_ids = mysql_fetch_array(); return $object_ids; } } class Category extends ForumObject { static private $table = 'categories'; static function getObjectIds ($field, $value) { parent::getObjectIds($field, $value, self::$table) } } -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Re: class problem :(
2009/5/29 Shawn McKenzie nos...@mckenzies.net Luke wrote: Right I've read the manual on this and all that so hopefully you find people can help. I have an abstract class with three children. The abstract is ForumObject and the three children are Thread, Category and Post and each have their own table so I wrote the following: abstract class ForumObject { static private $table; static function getObjectIds ($field, $value) { $query = SELECT id FROM {self::$table} WHERE $field = '$value'; $object_ids = mysql_fetch_array(); return $object_ids; } } class Category extends ForumObject { static private $table = categories; } That's just got the important bits for the sake of your eyes but basically the problem I'm having is calling Category::getObjectIds ($whatever, $whatever2); Seems to think that it's referring to ForumObject::$table rather than Category::$table? I looked into it and there seems to be something you can do with get_called_class() but unfortunately I'm stuck with 5.2.9 at the moment and that is new to 5.3. Any ideas? Perhaps there is a different way I could implement the classes - I would rather not have getObjectIds repeated three times! Thanks in advance, I didn't test this, just a thought. You'd still have to implement getObjectIds(), but it would be slim: abstract class ForumObject { static private $table; static function getObjectIds ($field, $value, $class) { $query = SELECT id FROM {$class::$table} WHERE $field = '$value'; $object_ids = mysql_fetch_array(); return $object_ids; } } class Category extends ForumObject { static private $table = 'categories'; static function getObjectIds ($field, $value) { parent::getObjectIds ($field, $value, __CLASS__) } } Or probably the same because you're defining $table anyway in the child class: abstract class ForumObject { static private $table; static function getObjectIds ($field, $value, $table) { $query = SELECT id FROM $table WHERE $field = '$value'; $object_ids = mysql_fetch_array(); return $object_ids; } } class Category extends ForumObject { static private $table = 'categories'; static function getObjectIds ($field, $value) { parent::getObjectIds($field, $value, self::$table) } } -- Thanks! -Shawn http://www.spidean.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php Ah that looks like the perfect solution! Trying now! Many thanks! -- Luke Slater http://dinosaur-os.com/ :O)
[PHP] Re: class Problem
Hi, when you want to access class member variables prefix them with $this- , otherwise they will be treated as local vars. hope that helps. Elias David Eggler [EMAIL PROTECTED] wrote in message [EMAIL PROTECTED]">news:[EMAIL PROTECTED]... I create a Instance fo the Class MenueItem in Menue. I change the value of one of its variables. But when i call the function within MenueItem there seems not to be a value: The intresting thing is it works with Other variables, even i cant echo them, they have a value Thanks for help class Menue { function Makechilde($somvalue) { echo Next_ID: . $Parent_ID . BR; //Works $MI-Parent_ID = $Parent_ID; echo Parent_ID: . $MI-Parent_ID . BR;//correct output $MI-write(); } } class MenueItem { var $ID_Menue; var $Name; var $Parent_ID; var $Next_ID; var $Prev_ID; function Write() { echo Parent_ID: . $MI-Parent_ID . BR; //(no output at all!) } } -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php