Jump to content

Recommended Posts

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 

 

 

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.

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.

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).

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.

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());
?>

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.