php 如何在php中调用父类方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12948837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to call parent class method in php
提问by Trialcoder
This is the working code, but i want to know without using another object(commented $foo) how could i use printItem()method of class Foousing the $barobject. New to oop programming concept so may be a weak thing to ask but really unable to locate :(
这是工作代码,但我想知道不使用另一个对象(注释$foo)如何使用使用该对象的printItem()方法。oop 编程概念的新手,所以可能是一个很弱的问题,但真的无法找到 :(class Foo$bar
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.
我使用范围解析操作人员使用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");
回答by cetver
define printItemas static method and you can use Foo::printItem("Mental Case");or call it in child method:
定义printItem为静态方法,您可以Foo::printItem("Mental Case");在子方法中使用或调用它:
public function printItem($string)
{
parent::printItem($string);
echo "This is in class Bar ". $string ."<br />";
}
回答by yasir hashmi
<?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
如果我们将 showName 方法的可见性更改为 public 或 protected,那么上面的结果将是:我在 extendTest 中的名字是 John
回答by CoreCoder
Make printItem a static method by adding "static" keyword. printItem($string) will be available to use without creating an object of this class.
通过添加“static”关键字使 printItem 成为静态方法。无需创建此类的对象即可使用 printItem($string)。
<?php
class Foo
{
static 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");

