在面向对象的php中调用方法

dsb*_*dsb 1 php oop

我相信这对你们大多数人来说都是一个愚蠢的问题.但是,我已经敲了很长时间.来自ASP.NET/C#,我现在正在尝试使用PHP.但是整个OOrintation给了我很多时间.

我有以下代码:

<html>

<head>
</head>
<body>

<?php 

echo "hello<br/>";

class clsA
{
    function a_func()
    {
        echo "a_func() executed <br/>";
    }
}

abstract class clsB
{
    protected $A;

    function clsB()
    {
        $A = new clsA();
        echo "clsB constructor ended<br/>";
    }
} 


class clsC extends clsB
{

    function try_this()
    {
        echo "entered try_this() function <br/>";
        $this->A->a_func();
    }
}

$c = new clsC();

$c->try_this();

echo "end successfuly<br/>";
?>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

根据我的简单理解,此代码应该包含以下行:

你好

clsB构造函数结束了

输入了try_this()函数

a_func()已执行

但是,它没有运行'a_func',我得到的是:

你好

clsB构造函数结束了

输入了try_this()函数

谁能发现问题?

提前致谢.

FtD*_*Xw6 9

你的问题在于:

$A = new clsA();
Run Code Online (Sandbox Code Playgroud)

在这里,您要clsA局部变量 分配一个新对象$A.你打算做的是将它分配给财产 $A:

$this->A = new clsA();
Run Code Online (Sandbox Code Playgroud)