how to get to parent overridden property from child?

learning php

in the php documentation it says:

The Scope Resolution Operator (also called Paamayim Nekudotayim) or in simpler terms, the double colon, is a token that allows access to static, constant, and overridden properties or methods of a class.

i get an error when i try to access overridden (not static) parent properties:

class foo
{
    public $bar = 'foobar';
}

class baz extends foo
{
    public $bar = 'bazbar';

    public function get_bar()
    {
        echo parent::$bar; //Fatal error:  Access to undeclared static property: foo::$bar
    }
}

$baz = new baz;
$baz->get_bar();
adamsmith

Fisrt, use :: with static properties, not instance properties.
Second, though you can do it with Reflection(see the following code), I don't see any point accessing parent instance properties, that's polymorphism is for.

class foo
{
    public $bar='foobar';
}
class bar extends foo
{
    public $bar='bazbar';
    function get_bar()
    {
        $thisClass = new ReflectionClass($this);
        $parentClass = $thisClass->getParentClass();
        $props = $parentClass->getDefaultProperties();
        return $props['bar'];
    }
}

$b = new bar();
echo $b->get_bar(); // foobar

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to get to parent overridden property from child?

From Java

How to get child process from parent process

From Dev

How to get a parent from a child FK?

From Dev

How to get the child factories from a parent factory

From Dev

How to get the parent and child ids from location

From Dev

How to get state from child to parent react

From Dev

how to get a grand child from parent in php

From Dev

Setting parent property from child

From Dev

Setting parent property from child

From Dev

How to block call overridden method from Child

From Dev

How to attribute a property from parent to the child in javascript? (OOP)

From Dev

Get overridden property attribute

From Dev

If you fork() and exec() from the child process, and wait in the parent, how does the parent get a return code from the child?

From Dev

Get child class from parent

From Dev

how to simply access property of parent class from child class by making object of child class

From Dev

Is it possible to access a parent property from a child that is in a collection?

From Dev

Set parent controller property from child controller

From Dev

Cannot set Parent property from child class

From Dev

Access parent property name from child class

From Dev

How to get the parent div id from the anchor child

From Dev

Using Debugger how to get child process's PID from Parent

From Dev

How to get a specific child divs value from parent using jquery

From Dev

How to get data from child to parent entity framework core?

From Dev

how to get the reference for checkbox element from both parent and child grids

From Dev

how to get mdi parent control from mdi child form

From Dev

how to get associative child nodeRef from parent nodeRef in Alfresco

From Dev

How get parent element attribute and child value from XML in java?

From Dev

Using Debugger how to get child process's PID from Parent

From Dev

django how to get field from child relationship into parent template

Related Related

  1. 1

    how to get to parent overridden property from child?

  2. 2

    How to get child process from parent process

  3. 3

    How to get a parent from a child FK?

  4. 4

    How to get the child factories from a parent factory

  5. 5

    How to get the parent and child ids from location

  6. 6

    How to get state from child to parent react

  7. 7

    how to get a grand child from parent in php

  8. 8

    Setting parent property from child

  9. 9

    Setting parent property from child

  10. 10

    How to block call overridden method from Child

  11. 11

    How to attribute a property from parent to the child in javascript? (OOP)

  12. 12

    Get overridden property attribute

  13. 13

    If you fork() and exec() from the child process, and wait in the parent, how does the parent get a return code from the child?

  14. 14

    Get child class from parent

  15. 15

    how to simply access property of parent class from child class by making object of child class

  16. 16

    Is it possible to access a parent property from a child that is in a collection?

  17. 17

    Set parent controller property from child controller

  18. 18

    Cannot set Parent property from child class

  19. 19

    Access parent property name from child class

  20. 20

    How to get the parent div id from the anchor child

  21. 21

    Using Debugger how to get child process's PID from Parent

  22. 22

    How to get a specific child divs value from parent using jquery

  23. 23

    How to get data from child to parent entity framework core?

  24. 24

    how to get the reference for checkbox element from both parent and child grids

  25. 25

    how to get mdi parent control from mdi child form

  26. 26

    how to get associative child nodeRef from parent nodeRef in Alfresco

  27. 27

    How get parent element attribute and child value from XML in java?

  28. 28

    Using Debugger how to get child process's PID from Parent

  29. 29

    django how to get field from child relationship into parent template

HotTag

Archive