How to use class protected methods in PHP

3 Answers

0 votes
class Test 
{
    public $pr = "Class property from class Test";
    
    public function __construct()
    {
        echo 'Constructor activated from class Test <br />';
    }
      
    public function __destruct()
    {
        echo '<br /> Destructor activated from class Test <br />';
    }
    
    public function setProperty($val)
    {
        $this->pr = $val;
    }
    
    protected function getProperty()
    {
        return $this->pr;
    }
    protected function f()
    {
        echo "Method f() from class " . __CLASS__ . "<br />";
    }
}
  
class MyNewClass extends Test
{
    public function __construct()
    {
        parent::__construct(); 
        echo 'Constructor activated from class MyNewClass <br />';
    }
}
 
   
$obj1 = new MyNewClass;

$obj1->setProperty("abcd");
$obj1->getProperty(); // protected, it can only be accessed within the class itself
$obj1->f();


/*
run:

Constructor activated from class Test 
Constructor activated from class MyNewClass 

Fatal error: Call to protected method Test::getProperty() from context ''
in C:\xampp\htdocs\he.seek4info.com\test.php on line 50

*/

 



answered Nov 3, 2015 by avibootz
0 votes
class Test 
{
    public $pr = "Class property from class Test";
    
    public function __construct()
    {
        echo 'Constructor activated from class Test <br />';
    }
      
    public function __destruct()
    {
        echo '<br /> Destructor activated from class Test <br />';
    }
    
    public function setProperty($val)
    {
        $this->pr = $val;
    }
    
    protected function getProperty()
    {
        return $this->pr;
    }
    protected function f()
    {
        echo "Method f() from class " . __CLASS__ . "<br />";
    }
}
  
class MyNewClass extends Test
{
    public function __construct()
    {
        parent::__construct(); 
        echo 'Constructor activated from class MyNewClass <br />';
    }
}
 
   
$obj1 = new MyNewClass;

$obj1->setProperty("xyz");
$obj1->f(); // protected, it can only be accessed within the class itself 
$obj1->getProperty(); 



/*
run:

Constructor activated from class Test 
Constructor activated from class MyNewClass 

Fatal error: Call to protected method Test::f() from context '' 
in C:\xampp\htdocs\he.seek4info.com\test.php on line 50


*/

 



answered Nov 3, 2015 by avibootz
0 votes
class Test 
{
    public $pr = "Class property from class Test";
    
    public function __construct()
    {
        echo 'Constructor activated from class Test <br />';
    }
      
    public function __destruct()
    {
        echo '<br /> Destructor activated from class Test <br />';
    }
    
    public function setProperty($val)
    {
        $this->pr = $val;
    }
    
    protected function getProperty()
    {
        return $this->pr;
    }
    protected function f()
    {
        echo "Method f() from class " . __CLASS__ . "<br />";
    }
}
  
class MyNewClass extends Test
{
    public function __construct()
    {
        parent::__construct(); 
        echo 'Constructor activated from class MyNewClass <br />';
    }
    public function runProtectedMethod()
    {
        return $this->getProperty();
    }
}
 
   
$obj1 = new MyNewClass;

$obj1->setProperty("some value");
echo $obj1->runProtectedMethod(); // protected, accessed within MyNewClass class with $this



/*
run:

Constructor activated from class Test 
Constructor activated from class MyNewClass 
some value 
Destructor activated from class Test 

*/

 



answered Nov 3, 2015 by avibootz

Related questions

2 answers 266 views
2 answers 309 views
309 views asked Nov 4, 2015 by avibootz
1 answer 187 views
187 views asked Nov 4, 2015 by avibootz
2 answers 221 views
221 views asked Oct 4, 2017 by avibootz
1 answer 328 views
2 answers 879 views
...