Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Prodentim Probiotics Specially Designed For The Health Of Your Teeth And Gums

Instant Grammar Checker - Correct all grammar errors and enhance your writing

Teach Your Child To Read

Powerful WordPress hosting for WordPress professionals

Disclosure: My content contains affiliate links.

31,089 questions

40,774 answers

573 users

How to overwriting inherited class methods in PHP

Learn & Practice SQL


732 views
asked Nov 3, 2015 by avibootz
edited Nov 4, 2015 by avibootz

2 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;
    }
    
    public function getProperty()
    {
        return $this->pr;
    }

    public function f()
    {
        echo "Method f() from class " . __CLASS__ . "<br />";
    }
}
  
class MyNewClass extends Test
{
    // Declaring __construct() method again in MyNewClass to overwrite
    public function __construct()
    {
        echo 'Constructor activated from class MyNewClass <br />';
    }
}
 
   
$obj1 = new MyNewClass;

$obj1->f();


/*
run:

Constructor activated from class MyNewClass 
Method f() from class Test

Destructor activated from class Test 

*/

 





answered Nov 3, 2015 by avibootz
edited 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;
    }
    
    public function getProperty()
    {
        return $this->pr;
    }

    public function f()
    {
        echo "Method f() from class " . __CLASS__ . "<br />";
    }
}
  
class MyNewClass extends Test
{
    // Declaring __construct() method again in MyNewClass to overwrite
    public function __construct()
    {
        echo 'Constructor activated from class MyNewClass <br />';
    }
    // Overwriting Inherited method f()
    public function f()
    {
        echo "Method f() from class " . __CLASS__ . "<br />";
    }  
}
 
   
$obj1 = new MyNewClass;

$obj1->f();


/*
run:

Constructor activated from class MyNewClass 
Method f() from class MyNewClass

Destructor activated from class Test 

*/

 





answered Nov 3, 2015 by avibootz

Related questions

1 answer 98 views
2 answers 112 views
112 views asked Oct 4, 2017 by avibootz
2 answers 130 views
...