Php abstract class contructor -


there's way initialize properties of abstract class in php example in java?

public abstract class person {      private string name;      public person(string name)     {         this.name = name;     } }  public class client extends person {     public client(string name)     {         super(name);     } } 

here's actual code : i'm getting error : "variable $name seems uninitialized"

abstract class person {     private $name;      public function person($name)     {         $this->$name = $name;     } }  class client extends person {     private $name;     public function client($name)     {         parent::person($name);     }  } 

one thing should aware of except of pointed in comments , in @ali torabi's answer, constructor parent class not called implicitly when child class constructor called.

from docs:

in order run parent constructor, call parent::__construct() within child constructor required.

abstract class person {     function __construct() {         echo 'person constructor called<br/>';     } }  class client extends person {     function __construct() {         parent::__construct();         echo 'client constructor called<br/>';     } } 

i guess important note, because in languages parent constructors called automatically. $name variable should protected imo if want available in classes extending person class. using private keyword make property available within class defined.


Comments

Popular posts from this blog

node.js - Using Node without global install -

How to access a php class file from PHPFox framework into javascript code written in simple HTML file? -

java - Null response to php query in android, even though php works properly -