Using $this when not in object context php

2019-08-15 03:06发布

问题:

I just started learning OOPS in php. I wrote a simple program for implementing inheritance. I am getting a fatal error of $this when not in object context. Can anyone explain me this error, what does it mean? here is my code:

<?php

    class human{

        public $gender;

        public function _construct($gender)
        {
            $this->gender=$gender;

            echo $this->get_gender();
        }

        public function get_gender()
        {
            return $this->gender;
        }


    }

    class person extends human{

        public $name;
        public $surname;

        public static function set_name($name)
        {
            $this->name=$name;
        }
        public static function set_surname($surname)
        {
            $this->surname=$surname;
        }

        public static function get_name()
        {
            return $this->name;
        }
        public static function get_surname()
        {
            return $this->surname;
        }
    }

    $john = new person('male');
    $john->set_name('John');
    $john->set_surname('Williams');
    echo $john->get_name().' '.$john->get_surname().'is a '.$john->get_gender();
    ?>

回答1:

There are two problems here:

  1. You have defined your methods as static. You should not do that as they are not, they depend on being called on an object as you want to use the objects non-static properties.

  2. You have a typo in your constructor function. The correct name for the constructor is __construct, notice the two _ at the beginning.



回答2:

In a static function, $this does not exist. You can refer to the class itself with self::. So, instead of $this->surname, use self::surname. As noted below, this will change your error to a new error as it requires the variables to be declared static as well.

Of course, you really need to figure out WHY you made those functions static. Should they be static?



回答3:

You're trying to access the $this property from a static method. You cannot do this, as anything static can be accessed without instantiating the class, therefore the $this variable would not make sense, as you are not in an actual class instance.

The this keyword refers to the current object you are working with, e.g. if you were to make 5 instances of person, each $this would refer to that specific instance of person, and you could change each objects properties freely.

When you use static you are not referring to any instance, you are simply accessing static variables and calling static methods of that class. All these variables and methods are 'class wide'. That means if you change the static property in one instance of the class, the static property will change in all the classes.

Another way to imagine this is, take when calling the new keyword, you create a new instance of the object. Every instance will have its own copy of all the properties and methods you describe in the class, EXCEPT for the static ones. Imagine that when you access a static method or a static property you are accessing one object always.

That's why it would not make sense to access $this from a static context. You are not referring to any specific instance, only the class itself.

I hope I've explained this well enough, it's a very important concept to grasp in OOP, and it does give some people a lot of trouble to understand the concept.