0

I may be having a basic OOP doubt but I believe it's worth sharing after hours of search.

For example: From my controller I want to retrieve data from an Entity Class... however, this class extends an Abstract that MAY do the job. Controller:

//Setting info to object
$entity->setName( 'Peter' );
$entity->setAnge( 33 );

//Retriving data in array
$data = $entity->getData();

Class Entity:

class Entity extends EntityAbstract {
    private $name;
    private $age;

    public function getData() {
      return parent::getData($this);//I'm passing this object to get its methods (or vars)
    }

    public class setName() { ... }
    public class getName() { ... }
    public class setAge() { ... }
    public class getAge() { ... }
}

And finally Abrastract Class (Parent).

class EntityAbstract {

    public function getData( $entity ) {
        //I'm not sure how to proceed here.
        //I've listed all child's methods, but can't list class_vars (private)
        $methods = get_class_methods( $entity );

        // By listing method, I've tryed to use a regular expression to
        // identify methods starting with 'get' and then setting an array but
        // this didn't work... I got a memory exceed error. 
        $data = array();
        // foreach() loop
        $data[ $valueKey ] = $entity->$methodGetOfEach();

        return $data;
    }
}

If perhaps there's some zend method to help me with this, but I believe this is a matter of OOP and PHP knowledge to solve.

Thanks for any suggestion.

1 Answer 1

1

Hi I'm not quite sure what you are trying to achieve, but out of a few things that are strange, trying to define a class inside a class as getters and setters...?

Anyway, why don't you do something like:

class Entity {

    protected $name;
    protected $age;

    public function getName() {
        return $this->name;
    }
    public function setName($name) {
        $this->name = $name;
    }

    public function getAge() {
        return $this->age;
    }
    public function setAge($age) {
        $this->age = intval($age);
    }

    public function toArray() {
            return = array(
                   $this->name,
                   $this->age
            );
    }
}

You could then do something like:

$entity = new Entity();

$entity->setName( 'Peter' );
$entity->setAge( 33 );

//Retriving data in array
$data = $entity->toArray();

Update:

Ah, now I get what you want to do.

You could achieve this the following way:

class EntityBase {

    protected $name;

    public function getName() {
        return $this->name;
    }
    public function setName($name) {
        $this->name = $name;
    }

    public function toArray() {
            return get_object_vars($this);
    }
}

class EntityExtend extends EntityBase {
    protected $age;

    public function getAge() {
        return $this->age;
    }
    public function setAge($age) {
        $this->age = intval($age);
    }
}

$entity = new EntityExtend();

$entity->setName( 'Peter' );
$entity->setAge( 33 );

//Retriving data in array
$data = $entity->toArray();

print_r($data);

It hasn't got anything to do with an actual abstract class type though. The magic of OOP.

Hope this helps!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for your help... For sure this would work. I was actually trying is NOT having to create a toArray (or getData) method every Entity class I create. Based on what I'm thinking, I could create new Entities Classes extending EntityAbstract and only setting its attributes and generationg getters and setters. That's all. Thanks.
Thanks Dave for you time. In this case I'll have to change my Entity vars from private to protected. That's fine. Thanks very much.
Glad I could help. And be aware: if you wanted vars in extended classes not to parsed by toArray(), you would only have to define them as private.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.