eldan88 Posted May 24, 2013 Share Posted May 24, 2013 Hey, I currently have a class named Photograph. In this class I have a attribute called $db_fields that contains an array of a list of values which are in my database . I also have a parent class called database_object.php. In database_object class I have a protected method named attributes(). The point of the attributed is to run a foreach() on the $db_fields attribute that is located on the photograph.php class. Every-time it tries to run a foreach i keep getting this warning. Access to undeclared static property: database_object::$db_fields. I need the the self::$db_fields to be the Photograph::$db_fields not $database_object::$db_fileds. Is this an example of "Late static bindings"? If so how can I correct this? Thanks Database Object parent class class database_object { protected function attributes() { $attributes = array(); foreach(self::$db_fields as $field) { if(property_exists($this, $field)) { $attributes[$field] = $this->$field; } } return $attributes; } } Photograph Class class Photograph extends database_object { protected static $table_name = "photographs"; protected $db_fields=array('id','filename','type','size','caption'); // This is the array I need the attribute function to go through public $id; public $filename; } Link to comment https://forums.phpfreaks.com/topic/278341-need-help-accessing-an-attribute-from-parent-class/ Share on other sites More sharing options...
requinix Posted May 24, 2013 Share Posted May 24, 2013 If you have PHP 5.3 you can foreach(static::$db_fields as $field) {Then make $db_fields static. Link to comment https://forums.phpfreaks.com/topic/278341-need-help-accessing-an-attribute-from-parent-class/#findComment-1431990 Share on other sites More sharing options...
eldan88 Posted May 24, 2013 Author Share Posted May 24, 2013 If you have PHP 5.3 you can foreach(static::$db_fields as $field) {Then make $db_fields static. requinix. Thank you that worked out! Link to comment https://forums.phpfreaks.com/topic/278341-need-help-accessing-an-attribute-from-parent-class/#findComment-1432072 Share on other sites More sharing options...
trq Posted May 25, 2013 Share Posted May 25, 2013 A little of topic but Photograph doesn't sound like a type of database_object, why is it extending it in the first place? Link to comment https://forums.phpfreaks.com/topic/278341-need-help-accessing-an-attribute-from-parent-class/#findComment-1432159 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.