PHP: is it possible to access static methods as follows: Object :: ChildObject :: method ()?

In JS, you can access methods this way: ParentObject.ChildObject.ChildObjMethod () - can this be done in PHP?

+3
source share
2 answers

::can be used to access static class members. But you can also access instantiated child objects in PHP using the regular arrow ->:

$parent->child->child_method();

See also Help - What does this character mean in PHP?

+1
source

. :: . , , ->.

<?php
    class Foo
    {
        static public $bar;

        static public function initStaticMembers()
        {
            self::$bar = new Bar();     
        }
    }

    class Bar
    {
        public function method()
        {
            echo "Hello World\n";
        }

    }

    Foo::initStaticMembers();
    Foo::$bar->method();

    Object::ChildObject::method();

-

    $o = Object::ChildObject;
    $o::method();

-. . PHP . , , .

, PHP. ,

    $o = new Baz();
    $o->method()->anotherMethod()->yetAnotherMethod();
    $o->someObjectReference->methodCall()->etc();

. ,

    $o->method();
    $o->someObjectReference;

, .

+2

Source: https://habr.com/ru/post/1782192/


All Articles