php - Can I create object of child class using method from parent class -


so have parent class:

    class table {      protected static $table_name;     protected static $tbl_columns=array();      public static function instance($row_from_db){         $object=new self;         foreach (self::$tbl_columns $key=>$property){             $object->$property =    $row_from_db[$property];         }         return $object;     }  } 

when call "instance" method in child class of class table generates object of class table not object of child class.

is there way code? or advice on how solve it? thanks

since php 5.3.0 use late static bindings:

$object = new static; foreach (static::$tbl_columns $key=>$property){ 

this works:

$class = get_called_class(); $object = new $class; foreach ($class::$tbl_columns $key=>$property){ 

Comments

Popular posts from this blog

javascript - Complete OpenIDConnect auth when requesting via Ajax -

c# - Replace text in MailItem Body -

apache - PHP Soap issue while content length is larger -