需要帮助理解 php 对象的各个方面

发布于 2024-11-08 14:38:35 字数 816 浏览 0 评论 0原文

我是 php 新手,需要一些帮助来理解我不明白的 php 对象的各个方面。在类ai中已经创建了类b和c,它们扩展了a,但我想做的是从类c访问类a中的公共字母变量,这样就可以从类c获取函数testb()。

非常感谢任何帮助,

   <?php

    class a {
        public $letters;
        function __construct() {
            $this->letters->b = new b();
            $this->letters->c = new c();
        }
    }   

    class b extends a {
        function __construct() {
            echo "hello world from b ";
        }

        private function testb() {
            echo "testing from b";
        }
    }

    class c extends a {
        function __construct() {
            echo "hello world from c ";
            $this->letters->b->testb();
        }
    }


    $a = new a();

?>

以下脚本呼应了我们的“来自 b 的 hello world”和“来自 c 的 hello world”,但它不会打印出“来自 b 的测试”...

i am new to php and is in need of some help to understand the aspects of php objects which i do not get. In class a i have made class b and c which extends a, but what i want to do is acess the public letters variable in class a from class c so in can get ot the function testb() from class c.

Any help is much appreciated guys

   <?php

    class a {
        public $letters;
        function __construct() {
            $this->letters->b = new b();
            $this->letters->c = new c();
        }
    }   

    class b extends a {
        function __construct() {
            echo "hello world from b ";
        }

        private function testb() {
            echo "testing from b";
        }
    }

    class c extends a {
        function __construct() {
            echo "hello world from c ";
            $this->letters->b->testb();
        }
    }


    $a = new a();

?>

the following script echos our "hello world from b" and "hello world from c" but it does not print out "testing from b"...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

稚然 2024-11-15 14:38:35

PHP 对象的一个​​问题是继承被捏造了(只是一点点)。当您实例化其中一个子对象时,不会隐式调用父对象的构造函数。相反,您必须显式调用父对象的构造函数。

class b extends a {
...
    public __construct() {
        parent::__construct();
        echo("Hello From B!");
    }
...
}

在类 c 构造函数中,$this->letters 不是对象,并且尚未初始化,因为尚未调用 a 构造函数。

如果您只是在玩弄对象,那么请忽略这部分,但您已经接近获得循环层次结构了。如果您添加我建议的更改,我相信如果您尝试直接实例化 B 或 C 对象,您的代码将会爆炸。

A gotcha of PHP Objects is that Inheritance is fudged (just a bit).. The constructor of a parent object is not implicitly called when you instantiate one of it's child objects. Instead you must explicitly call the Parent Object's constructor.

class b extends a {
...
    public __construct() {
        parent::__construct();
        echo("Hello From B!");
    }
...
}

In your Class c constructor, $this->letters is not an object and has not been initialized since the a constructor has not been called.

If you're just playing around with objects, then ignore this part, but you're close to getting a circular hierarchy. If you add the changes that I suggested, I believe your code will explode if you try to instantiate either a B or a C object directly.

み零 2024-11-15 14:38:35

您无法从类 c 中调用 testb 的原因是 testb 的方法签名将其声明为 private - 意味着它只能在 b 实例中访问。将可见性更改为默认(无修饰符)或public

例如:

function testb() {
    echo "testing from b";
}

或者:

public function testb() {
    echo "testing from b";
}

编辑:

问题是您没有从 c 的构造函数中调用超级构造函数 - 因此 $this->b 的值未初始化。将 c 的构造函数更改为:

function __construct() {
    parent::__construct();
    echo "hello world from c ";
    $this->letters->b->testb();
}

但是不幸的是,这将创建一个无限循环,其中 a 的构造函数正在实例化 cc 的构造函数> 正在调用 a 的构造函数。为什么你们的字母类别会相互延伸?

您还需要更改 b 类中 testb 的可见性。

The reason you can't call testb from within the class c is that the method signature of testb declares it as private - meaning it can only be accessed within b instances. Change the visibility to default (no modifier) or public.

E.g.:

function testb() {
    echo "testing from b";
}

Or:

public function testb() {
    echo "testing from b";
}

Edit:

The problem is that you haven't called the super constructor from the constructor of c - the value of $this->b is therefore not initialised. Change the constructor of c to:

function __construct() {
    parent::__construct();
    echo "hello world from c ";
    $this->letters->b->testb();
}

Unfortunately, though, this will create an infinite loop where the constructor of a is instantiating c and the constructor of c is calling up to the constructor of a. Why do your letter classes even extend one another?

You also need to change the visibility of testb within the b class.

山色无中 2024-11-15 14:38:35

好的,如果您从 b 扩展类 c ,您可以调用该方法,否则不是因为您是从类 a 而不是从 c 扩展它。

Ok, if you extend class c from b you can call the method otherwise not as you are extending it from class a not from c..

却一份温柔 2024-11-15 14:38:35

在类 a 中,您正在构造类 c 的新对象。

这个新对象没有属性$this->letters->b,它只有一个未初始化的属性$this->letters

请注意,新对象从其扩展的类继承了属性 $letters,但它不继承实例化 a 对象的任何值。所以没有 $this->letters->b 也没有函数 $this->letters->b->testb() 可以调用。

In your class a you are constructing a new object of class c.

This new object has no property $this->letters->b, it just has an uninitialized property $this->letters.

Note that the new object inherits the property $letters from the class it extends, but it does not inherit any values of an instantiated a object. So there is no $this->letters->b and no function $this->letters->b->testb() to call.

好久不见√ 2024-11-15 14:38:35

“c”扩展了“a”并且不继承

您的示例将返回的

“b”方法

致命错误:调用成员函数
testb() 在非对象上

$this->字母->b->testb();

"c" extends "a" and doesn't inherit "b"s method

your example will return

Fatal error: Call to a member function
testb() on a non-object

for

$this->letters->b->testb();

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文