Using $ this if not in php object context

I just started learning OOPS in php. I wrote a simple program to implement inheritance. I am getting fatal error $ this if not in object context. Can anyone explain this error to me what it means? 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


source to share


3 answers


There are two problems here:



  • You have defined your methods as static

    . You shouldn't do this as it doesn't, they depend on the call to the object as you want to use objects with non-static properties.

  • You have a typo in your constructor function. Correct name for the constructor __construct

    , notice the two _

    at the beginning.

+1


source


$this

Doesn't exist in a static function . You can refer to the class itself with self::

. So $this->surname

use instead 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 these functions static. Should they be static?

+2


source


You are trying to access the $ this property from a static method. You cannot do this as any static access can be accessed without instantiating the class, so the $ this variable is meaningless since you are not in the actual class instance.

This keyword refers to the current object you are working with, for example. if you were to make 5 instances of a person, every dollar would refer to that specific instance of a person and you can freely change the properties of each object.

When you use static, you are not referring to any instance, you just access the static variables and call the static methods of that class. All of these variables and methods are "broad". This means that if you change a static property in one instance of a class, the static property will change in all classes.

Another way to think about this is to accept when you call the new keyword, you are creating a new instance of the object. Each instance will have its own copy of all properties and methods that you describe in the class, EXCEPT for static ones. Imagine that when you access a static or static property, you are always accessing the same object.

This is why it doesn't make sense to access $ this from a static context. You don't mean any particular instance, just the class itself.

I hope I have explained this well enough, it is a very important concept to understand OOP, and it gives some people a lot of trouble to understand the concept.

+1


source







All Articles