removing parent class properties from child object

  class, model, oop, php

im trying to create a ORM for practice , i have a base class (model) which i use to extends my other models in this example User class

here is simplified version of my code , please ignore syntax errors

class Model {

    private  $condition = [] ;
    private  $order     = [] ;

    public static function __callStatic($method, $arguments)
    {
        $caller_class  =  get_called_class();
        $caller_object = new $caller_class();
        return  $caller_object->{$method}($arguments) ;
    }

    public function __call($method, $arguments) {

        $argument = count($arguments) == 1 && count($arguments[0]) > 1 ? $arguments[0]  :$arguments ;


        switch ($method)
        {
            case 'where' :
            {
                if( count($argument) != 2 )
                    throw new Exception("invalid arguments");

                $this->condition []  = implode('=' , $argument ) ;
                return $this ;

            }
            case 'get' :
            {
                return $this->query();
            }
        }


    }

    private function query(){

        $query = "select * from $this->table ";

        if(!empty($this->condition))
            $query .= ' WHERE '.implode(' AND '  ,   $this->condition );

        $results = run_query($query); // running with pdo 

        $caller_class  =  get_called_class();
        foreach ($results as $row  )
        {
            $caller_object = new $caller_class();

            foreach ($row as $k=>$v)
                $caller_object -> $k = $v  ;

            $export[] = $caller_object ;

        }

        return $export;

    }


}

user model and usage

class User extends Model
{
   protected $table = 'users';
}

$users = User::get();

dump($users);

the problem is i get condition and order properties in my final export objects

(
    [0] => User Object
        (
            [table:protected] => users
            [condition:Model:private] => Array
                (
                )

            [order:Model:private] => Array
                (
                )

            [id] => 2
            [username] => jack
            [email] => [email protected]
            [score] => 2
            [city] => rio
        )

)

how can i get rid of private and protected properties specially the one from base model in the final export ? (condition , order , table )

basically i want my final export to be

(
    [0] => User Object
        (
            [id] => 2
            [username] => jack
            [email] => [email protected]
            [score] => 2
            [city] => rio
        )

)

i can unset them manually but i thought maybe there is a better way

Source: Ask PHP

LEAVE A COMMENT