Fatal error: Uncaught Error: Call to private method person::get_pinn_number() from global scope in /var/www/html/php8/ch7/killerphp/pag-18/index.php:13 Stack trace: #0 {main} thrown in /var/www/html/php8/ch7/killerphp/pag-18/index.php on line 13

index.php

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>OOP In PHP</title>
        <?php include("class_lib.php");?>
    </head>
    <body>
        <?php
        $stefan = new person("Stefan Mischook");
        $jimmy = new person("Nick Waddles");

        echo "Stefan's full name: " . $stefan->get_pinn_number();
        echo "</br>";
        echo "Nick's full name: " . $jimmy->get_pinn_number();

        /*
        Since $pinn_number was declared private, this line of code
        will generate an error. Try it out! 
        */
        echo "Tell me private stuff: " . $stefan->$pinn_number;
        echo "Tell me private stuff: " . $jimmy->$pinn_number;
        ?>
    </body>
</html>

if is
public $pinn_number;
public function get_pinn_number(){
return $this->$pinn_number;
}


this error disappear
Fatal error: Uncaught Error: Call to private method person::get_pinn_number() from global scope in /var/www/html/php8/ch7/killerphp/pag-18/index.php:13 Stack trace: #0 {main} thrown in /var/www/html/php8/ch7/killerphp/pag-18/index.php on line 13


there is another error
Warning: Undefined variable $pinn_number in /var/www/html/php8/ch7/killerphp/pag-18/class_lib.php on line 13

Warning: Undefined property: person::$ in /var/www/html/php8/ch7/killerphp/pag-18/class_lib.php on line 13
Stefan's full name:

Warning: Undefined variable $pinn_number in /var/www/html/php8/ch7/killerphp/pag-18/class_lib.php on line 13

Warning: Undefined property: person::$ in /var/www/html/php8/ch7/killerphp/pag-18/class_lib.php on line 13
Nick's full name:
Warning: Undefined variable $pinn_number in /var/www/html/php8/ch7/killerphp/pag-18/index.php on line 21

Warning: Undefined property: person::$ in /var/www/html/php8/ch7/killerphp/pag-18/index.php on line 21
Tell me private stuff:
Warning: Undefined variable $pinn_number in /var/www/html/php8/ch7/killerphp/pag-18/index.php on line 22

Warning: Undefined property: person::$ in /var/www/html/php8/ch7/killerphp/pag-18/index.php on line 22
Tell me private stuff:
class_lib.php

<?php
 class person {
    var $name;
    public $height;
    protected $social_insurance;
    private $pinn_number;

    function __construct($persons_name){
        $this->name = $persons_name;
    }

    private function get_pinn_number(){
        return $this->$pinn_number;
    }
 }
?>
             /*
            Since $pinn_number was declared private, this line of code
            will generate an error. Try it out! 
            */
            echo "Tell me private stuff: " . $stefan->$pinn_number;

    Because it would be called $stefan->pinn_number the same way you write $this->name elsewhere.

        private function get_pinn_number(){
            return $this->$pinn_number;
        }

    if is
    public $pinn_number;
    public function get_pinn_number(){
    return $this->$pinn_number;
    }

    this error disappear

    If you know the answer then what is the problem?

      there is another error
      Warning: Undefined variable $pinn_number in /var/www/html/php8/ch7/killerphp/pag-18/class_lib.php on line 13

      Warning: Undefined property: person::$ in /var/www/html/php8/ch7/killerphp/pag-18/class_lib.php on line 13
      Stefan's full name:

      Warning: Undefined variable $pinn_number in /var/www/html/php8/ch7/killerphp/pag-18/class_lib.php on line 13

      Warning: Undefined property: person::$ in /var/www/html/php8/ch7/killerphp/pag-18/class_lib.php on line 13
      Nick's full name:
      Warning: Undefined variable $pinn_number in /var/www/html/php8/ch7/killerphp/pag-18/index.php on line 21

      Warning: Undefined property: person::$ in /var/www/html/php8/ch7/killerphp/pag-18/index.php on line 21
      Tell me private stuff:
      Warning: Undefined variable $pinn_number in /var/www/html/php8/ch7/killerphp/pag-18/index.php on line 22

      Warning: Undefined property: person::$ in /var/www/html/php8/ch7/killerphp/pag-18/index.php on line 22
      Tell me private stuff:
      class_lib.php

        In case you're still not seeing it...

        class Test {
          public $foo = 'Foo';
          public $bar = 'Bar';
        }
        $test = new Test();
        $foo = 'bar'; // note this assignment to local variable $foo
        echo $test->foo; // normal usage
        // Foo
        echo $test->$foo; // In this case, actually references $test->bar
        // Bar
        echo $test->$bar; // $bar not defined locally, so just plain wrong here
        // PHP Warning:  Undefined variable $bar in php shell code ...
        
          Write a Reply...