Welcome to collectivesolver - Programming & Software Q&A. A website you can trust. All programs tested and works. Contact: aviboots(AT)netvision.net.il
Hostgator 1 Penny

BlueHost Web Hosting

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

Ecommerce Software - Best Ecommerce Platform Made for You - Free Trial

getflywheel wordpress hosting


Distribute your Resume to 500+ Job Boards & Recruiters


7,780 questions

10,864 answers

573 users

How to use self in class to refer to the current class in PHP

1 Answer

0 votes
class Person {
    private $name;
 
    public function __construct($name) {
        $this->name = $name;
    }
 
    public function getName() {
        return $this->name;
    }
 
    public function Profession($p) {
        echo self::getName(). " - " . $p . "<br/>";
    }
}
 
class Genius extends Person {
    public function __construct($name) {
        parent::__construct($name);
    }
}
 
$obj = new Genius("Albert Einstein");
echo $obj->getName() . "<br />";
$obj->Profession("Theoretical Physicist");
 
  
/*
run: 
  
Albert Einstein
Albert Einstein - Theoretical Physicist
   
*/

 




answered Sep 9, 2017 by avibootz
edited Sep 11, 2017 by avibootz
...