Php继承问题

时间:2009-12-27 07:06:46

标签: php oop

如何使用我在子类的父类中定义的函数?

例如,如果我使用类似下面的类

<?php

class mat

{

function square($x)

{

return $x *$x;

}

}


class matchild extends mat

{

function doublesquare($x)
{

return square($x) * square($x)

}

}

?>

如果我尝试上述操作,我会收到错误消息,说明没有定义方形函数。

答案和建议表示赞赏。

4 个答案:

答案 0 :(得分:10)

您需要使用this

return $this->square(x) * $this->square(x);

查看关于类和对象的PHP basic documentation

答案 1 :(得分:1)

您的代码段存在一些问题。但你要找的答案是:

$this->square()

答案 2 :(得分:0)

parent::square(x) * parent::square(x)

答案 3 :(得分:-2)

在matchild的构造函数中调用parent::__construct()

class matchild extends mat
{
    function __construct()
    {
        parent::__construct();
    }
}

然后,您可以使用$this->

调用父级中包含的任何方法
相关问题