static properites does not exist into the object

Dhairya Lakhera
class SubObject
{
    static $static_a= 0;
    public $normal_a=0;

    public function __construct() {
        ++$this->normal_a;
        ++self::$static_a;
    }

}

$obj1 = new SubObject();
print_r($obj1);

result is :

SubObject Object
(
    [normal_a] => 1
)

My question is that why its not display output as:

SubObject Object
(
    [normal_a] => 1
    [static_a] => 1
)

Does static properites does not exist into the object ? Static variable or property are the way to preserver value of the variable within the context of different instance?

BareNakedCoder

The static properties are attributes of the class (all instances), not an attribute of a specific instance. Here's another class ...

class Dog {
    public static $species = 'mammal';
    public $furColour;

    public function __construct($furColour) {
        $this->furColour = $furColour;
   }
}
$myDog = new Dog('brown');

All dogs are mammals, in other words the entire "class" of dogs are mammals, so it makes sense to store the $species attribute at the class level (not in every instance of the class). Not all dogs have the same fur colour, that is an attribute of a specific instance of the class know as "Dog".

So, as decided by whoever designed the print_r function, it only prints attributes specific to the instance, not all the attributes of the entire class (or set of all instances). This design decision makes sense. Especially for classes that, for example, define 10's or even 100's of attributes to be used a constants: you don't want to see all these every time you print_r to debug.

FYI, if your app has a real need to get the static values, I think this works

print_r( (new ReflectionClass('SubObject'))->getStaticProperties() );

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

What does "Static factories returned object need not exist" mean?

From Dev

Getting properites of static class in static function?

From Dev

JSTL: Does Object Exist

From Dev

Accessing JSON array's through object properites

From Dev

Accessing JSON array's through object properites

From Dev

Firebase image object does not exist?

From Dev

"... matching query does not exist." error, but object clearly does exist

From Dev

Static regex object or does it matter?

From Dev

Mockery & PHPUnit: method does not exist on this mock object

From Dev

Object has value but does not appear to exist?

From Dev

ActiveModelSerializer: return object of nils if association does not exist

From Dev

LdapConnection SearchRequest throwing object does not exist error

From Dev

Does there exist a mutable URL/URI object in Java?

From Dev

method does not exist on this mock object - Laravel , Mockery

From Dev

How to get around property does not exist on 'Object'

From Dev

LdapConnection SearchRequest throwing object does not exist error

From Dev

Override issue: does exist a method in Object to override?

From Dev

Object does not exist memo from sql query

From Dev

Mockery & PHPUnit: method does not exist on this mock object

From Dev

Does this object exist? c++ scope/inheritance

From Dev

Validation for Laravel object variable that is empty or does not exist

From Dev

Django tests apis model object does not exist

From Dev

__callStatic does not call if there exist a non-static function

From Dev

Python append json object in file, guard if object does not exist

From Dev

Does deserialized object preserve static values?

From Dev

Method "currentAncestor" for object "Knp\Menu\MenuItem" does not exist in Sonata

From Dev

Does "Object Expression" exist in C# like in F#?

From Java

Primary key property 'name' does not exist on object 'RealmSwiftPermissionRole'

From Dev

Javascript creating a normal variable fallback if object property does not exist

Related Related

  1. 1

    What does "Static factories returned object need not exist" mean?

  2. 2

    Getting properites of static class in static function?

  3. 3

    JSTL: Does Object Exist

  4. 4

    Accessing JSON array's through object properites

  5. 5

    Accessing JSON array's through object properites

  6. 6

    Firebase image object does not exist?

  7. 7

    "... matching query does not exist." error, but object clearly does exist

  8. 8

    Static regex object or does it matter?

  9. 9

    Mockery & PHPUnit: method does not exist on this mock object

  10. 10

    Object has value but does not appear to exist?

  11. 11

    ActiveModelSerializer: return object of nils if association does not exist

  12. 12

    LdapConnection SearchRequest throwing object does not exist error

  13. 13

    Does there exist a mutable URL/URI object in Java?

  14. 14

    method does not exist on this mock object - Laravel , Mockery

  15. 15

    How to get around property does not exist on 'Object'

  16. 16

    LdapConnection SearchRequest throwing object does not exist error

  17. 17

    Override issue: does exist a method in Object to override?

  18. 18

    Object does not exist memo from sql query

  19. 19

    Mockery & PHPUnit: method does not exist on this mock object

  20. 20

    Does this object exist? c++ scope/inheritance

  21. 21

    Validation for Laravel object variable that is empty or does not exist

  22. 22

    Django tests apis model object does not exist

  23. 23

    __callStatic does not call if there exist a non-static function

  24. 24

    Python append json object in file, guard if object does not exist

  25. 25

    Does deserialized object preserve static values?

  26. 26

    Method "currentAncestor" for object "Knp\Menu\MenuItem" does not exist in Sonata

  27. 27

    Does "Object Expression" exist in C# like in F#?

  28. 28

    Primary key property 'name' does not exist on object 'RealmSwiftPermissionRole'

  29. 29

    Javascript creating a normal variable fallback if object property does not exist

HotTag

Archive