vendredi 4 mars 2016

How to get an array of property names from base class in an inheritance context with PHP?

Here I have a bunch of entity classes composed like this:

<?php
class BaseModel {
    protected $Id;
    protected $CreateDate;
    protected $LastUpdateDate;
    // public setters, getters and validation methods
    public function getClassFields(){
        // how to get an array containing all property names,
        // including those from inherited classes?
        $array = (array) $this; //this do not work =(
        return $array;
    }
}

class FooModel extends BaseModel {
    protected $Bar;
    protected $Baz;
    protected $Loo;
    // public setters, getters and validation methods
}
?>

I want to get an array containing ["Id", "CreateDate", "LastUpdateDate", "Bar", "Baz", "Loo"]. How do I accomplish this?

What I've tried:

When I try to do an array cast $array = (array) new FooModel() from outside of the class or $array = (array) $this from inside of base class, both do not work... I think that get_object_vars function don't work since all properties are protected.

Should I use reflection instead?

Thank you in advance!





Aucun commentaire:

Enregistrer un commentaire