Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to call parent class method in php

This is the working code, but i want to know without using another object(commented $foo) how could i use printItem() method of class Foo using the $bar object. New to oop programming concept so may be a weak thing to ask but really unable to locate :(

I use scope resolution operator to use printItem() of Foo class, now my query is when we can use this functionality then what is the use of creating objects ? When to use scope resolution operators in proper coding environment.

<?php

class Foo
{
    public function printItem($string)
    {
        echo "This is in class Foo ". $string ."<br />";
    }

    public function printPHP()
    {
        echo "PHP is great "."<br />";
    }
}

class Bar extends Foo
{
    public function printItem($string)
    {
        echo "This is in class Bar ". $string ."<br />";
    }
}       

//$foo = new Foo;
$bar = new Bar;

$bar->printPHP();
$bar->printItem("Bar class object");
//Foo::printItem("Mental Case");
like image 823
Trialcoder Avatar asked Oct 18 '12 07:10

Trialcoder


People also ask

How do you call a parent class in PHP?

In order to run a parent constructor, a call to parent::__construct() within the child constructor is required. If the child does not define a constructor then it may be inherited from the parent class just like a normal class method (if it was not declared as private). $obj = new OtherSubClass();

How do you call a method of parent class?

Calling Parent class method after method overriding Using Classname: Parent's class methods can be called by using the Parent classname. method inside the overridden method. Using Super(): Python super() function provides us the facility to refer to the parent class explicitly.

How do you call parent method from child object?

If you override a parent method in its child, child objects will always use the overridden version. But; you can use the keyword super to call the parent method, inside the body of the child method.


2 Answers

define printItem as static method and you can use Foo::printItem("Mental Case"); or call it in child method:

public function printItem($string)
{
    parent::printItem($string);
    echo "This is in class Bar ". $string ."<br />";
}
like image 123
cetver Avatar answered Sep 22 '22 16:09

cetver


<?php
class test {

    public function __construct() {}

    public function name() {
       // $this->xname('John');
          $this->showName('John');
    }

    private function showName($name) {
        echo 'my name in test is '.$name;
    }
}

class extendTest extends test {

    public function __construct() {
        parent::__construct();
    }

    private function showName($name) {
        echo 'my name in extendTest is '.$name;
    }
}

$test = new extendTest();
$test->name();
?>

result: my name in test is John

If we change visibility of the showName method to public or protected then the result of the above will be: my name in extendTest is John

like image 29
yasir hashmi Avatar answered Sep 22 '22 16:09

yasir hashmi