Using $this when not in object context php

Amit Kaushal

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();
    ?>
jeroen

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.

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 access an object from the topology context into a bolt when using storm?

From Dev

Using $this when not in object context error within array_walk

From Dev

Error: Using $this when not in object context

From Dev

Using $this when not in object context - Laravel 4

From Dev

ATK4: Error: Using $this when not in object context

From Dev

Using $this when not in object context - Laravel 4 PHP 5.4.12

From Dev

PHP $this when not in object context for set public variable from out of class

From Dev

Fatal error: Using $this when not in object context explanation?

From Dev

"Using $this when not in object context" appears in Silex/phpunit case

From Dev

Using $this when not in object context with ReflectionFunction->invoke()

From Dev

CodeIgniter "Using $this when not in object context" in function

From Dev

Error Using $this when not in object context

From Dev

Using $this when not in object context calling method inside class

From Dev

PHP - How to solve error "using $this when not in object context"?

From Dev

Zend Framework 2: Fatal error - using $this when not in object context

From Dev

Using $this when not in object context without the use of static methods

From Dev

Using $this when not in object context?

From Dev

ERROR: using '$this' when not in object context

From Dev

Using $this when not in object context - Laravel 4 PHP 5.4.12

From Dev

What causes this: Using $this when not in object context

From Dev

Fatal error: Using $this when not in object context

From Dev

Using $this when not in object context? PHP

From Dev

Using $this when not in object context php

From Dev

PHP : "Fatal error: Using $this when not in object context in"

From Dev

Using $this when not in object context into the same class

From Dev

Using $this when not in object context in Codeigniter

From Dev

OOP Fatal error: Using $this when not in object context

From Dev

PHP OOP - Uncaught Error: Using $this when not in object context

From Dev

Using $this when not in object context Yii2

Related Related

  1. 1

    How to access an object from the topology context into a bolt when using storm?

  2. 2

    Using $this when not in object context error within array_walk

  3. 3

    Error: Using $this when not in object context

  4. 4

    Using $this when not in object context - Laravel 4

  5. 5

    ATK4: Error: Using $this when not in object context

  6. 6

    Using $this when not in object context - Laravel 4 PHP 5.4.12

  7. 7

    PHP $this when not in object context for set public variable from out of class

  8. 8

    Fatal error: Using $this when not in object context explanation?

  9. 9

    "Using $this when not in object context" appears in Silex/phpunit case

  10. 10

    Using $this when not in object context with ReflectionFunction->invoke()

  11. 11

    CodeIgniter "Using $this when not in object context" in function

  12. 12

    Error Using $this when not in object context

  13. 13

    Using $this when not in object context calling method inside class

  14. 14

    PHP - How to solve error "using $this when not in object context"?

  15. 15

    Zend Framework 2: Fatal error - using $this when not in object context

  16. 16

    Using $this when not in object context without the use of static methods

  17. 17

    Using $this when not in object context?

  18. 18

    ERROR: using '$this' when not in object context

  19. 19

    Using $this when not in object context - Laravel 4 PHP 5.4.12

  20. 20

    What causes this: Using $this when not in object context

  21. 21

    Fatal error: Using $this when not in object context

  22. 22

    Using $this when not in object context? PHP

  23. 23

    Using $this when not in object context php

  24. 24

    PHP : "Fatal error: Using $this when not in object context in"

  25. 25

    Using $this when not in object context into the same class

  26. 26

    Using $this when not in object context in Codeigniter

  27. 27

    OOP Fatal error: Using $this when not in object context

  28. 28

    PHP OOP - Uncaught Error: Using $this when not in object context

  29. 29

    Using $this when not in object context Yii2

HotTag

Archive