如何在php中调用父类方法

Tri*_*der 7 php oop inheritance scope-resolution

这是工作代码,但我想知道如何不使用另一个对象(评论$foo)如何使用该对象的printItem()方法.新的oop编程概念所以可能是一个弱的问题,但真的无法找到:(class Foo$bar

我使用范围解析操作人员使用printItem()Foo class,现在我的查询时,我们可以使用这个功能,那么什么是使用创建的对象?何时在适当的编码环境中使用范围解析运算符.

<?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");
Run Code Online (Sandbox Code Playgroud)

cet*_*ver 16

定义printItem为静态方法,您可以Foo::printItem("Mental Case"); 在子方法中使用或调用它:

public function printItem($string)
{
    parent::printItem($string);
    echo "This is in class Bar ". $string ."<br />";
}
Run Code Online (Sandbox Code Playgroud)