LostInTheWoods Posted August 20, 2008 Share Posted August 20, 2008 Hi, First post, so be nice All my objects have getters and setters, and a method called getAllProperties() that loops through the objects properties and returns them in an array, like so: public function getAllProperties() { $all = array(); foreach($this as $var => $value) { $all[$var] = $value; } return $all; } But to avoid lots of repetition I thought I would put this method in an abstract class so all my objects that extend the abstract class can use the method. But no. For some reason it is not possible to loop over the properties of the (non abstract) object. Other stuff works, like calling a getter, but no the looping. Any ideas? Many Thanks, LITW Link to comment https://forums.phpfreaks.com/topic/120508-solved-getallproperties-in-an-abstract-class/ Share on other sites More sharing options...
knowj Posted August 20, 2008 Share Posted August 20, 2008 Have you looked at the permissions of the items? Just out of curiosity what context would you need to use this method? Link to comment https://forums.phpfreaks.com/topic/120508-solved-getallproperties-in-an-abstract-class/#findComment-620979 Share on other sites More sharing options...
LostInTheWoods Posted August 20, 2008 Author Share Posted August 20, 2008 The properties are private. But my thought was that the method is being inherited, and it can be used as if it was in the object class. I use this method mainly for debugging, and my thought was I could just go to the abstract class at the end of development and remove it, rather than hunting through my objects. Link to comment https://forums.phpfreaks.com/topic/120508-solved-getallproperties-in-an-abstract-class/#findComment-620981 Share on other sites More sharing options...
knowj Posted August 20, 2008 Share Posted August 20, 2008 It may be worth trying a test with the items set to public. PHP can sometimes do some strange things. One i recently found is that you cannot have a private __destruct(), it results in a fatal error. I was struggling to figure out what the method would be used for. Must come in handy for debugging. Link to comment https://forums.phpfreaks.com/topic/120508-solved-getallproperties-in-an-abstract-class/#findComment-620985 Share on other sites More sharing options...
LostInTheWoods Posted August 20, 2008 Author Share Posted August 20, 2008 It may be worth trying a test with the items set to public. PHP can sometimes do some strange things. Now you said that a penny dropped. I changed the private to protected and it works. Which kind of makes sense.... I think. So the method is being 'run' from the abstract class so it needs to have access to the properties... so protected works (as does of course public does too). Link to comment https://forums.phpfreaks.com/topic/120508-solved-getallproperties-in-an-abstract-class/#findComment-620993 Share on other sites More sharing options...
redbullmarky Posted August 20, 2008 Share Posted August 20, 2008 correct. to clarify: private properties (and methods) are only accessible to the class they're defined. to access properties in parent/child classes, they will need to be set to at least protected. to access them from an independent class/object, they'd need to be public. Link to comment https://forums.phpfreaks.com/topic/120508-solved-getallproperties-in-an-abstract-class/#findComment-621118 Share on other sites More sharing options...
448191 Posted August 21, 2008 Share Posted August 21, 2008 You could use reflection to iterate over all methods, adding ones that start with 'get', or if that wont work, you could iterate over all properties, and assemble the method name from it. Link to comment https://forums.phpfreaks.com/topic/120508-solved-getallproperties-in-an-abstract-class/#findComment-622245 Share on other sites More sharing options...
448191 Posted August 22, 2008 Share Posted August 22, 2008 Here's an example. It doesn't include the parent's properties if they are private though. Alternatively, you can use getMethods(), but then you might get some false positives. Maybe now still though. In all, I just don't think it is a very good idea. Here's it anyway, if you insist on using it, go ahead. <?php abstract class Foo { protected $_meh; protected $_blah; public function getMeh() { return $this->_meh; } public function getBlah() { return $this->_blah; } public function getAllProperties() { $all = array(); $refl = new ReflectionObject($this); foreach($refl->getProperties() as $prop) { $getter = 'get' . ucfirst(!$prop->isPublic()? substr($prop->getName(), 1) : $prop->getName()); if(method_exists($this, $getter)) { $all[$prop->getName()] = call_user_func(array($this, $getter)); } } return $all; } } class FooBar extends Foo { private $_ugh; private $_bah; public function getUgh() { return $this->_ugh; } public function getBah() { return $this->_bah; } } $foobar = new FooBar(); var_dump($foobar->getAllProperties()); ?> Link to comment https://forums.phpfreaks.com/topic/120508-solved-getallproperties-in-an-abstract-class/#findComment-622805 Share on other sites More sharing options...
LostInTheWoods Posted August 22, 2008 Author Share Posted August 22, 2008 Well, thanks for that. First question on this forum and I get a example solution! Thanks again, LITW Link to comment https://forums.phpfreaks.com/topic/120508-solved-getallproperties-in-an-abstract-class/#findComment-622851 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.