How to define and use class constructor in PHP

1 Answer

0 votes
class Test 
{
    public $pr = "Class property";
  
    public function __construct()
    {
        echo __CLASS__, ' constructor activated <br />';
    }
  
    public function setProperty($val)
    {
        $this->pr = $val;
    }
  
    public function getProperty()
    {
        return $this->pr;
    }
}
 
$obj1 = new Test;
 
echo $obj1->getProperty();
 
$obj1->setProperty("New property value");
 
echo "<br />";
echo $obj1->getProperty();
  
/*
run:
 
Test constructor activated
Class property
New property value 
 
*/

 



answered Oct 23, 2015 by avibootz

Related questions

1 answer 214 views
1 answer 144 views
1 answer 197 views
2 answers 239 views
1 answer 183 views
183 views asked May 30, 2016 by avibootz
1 answer 234 views
...