1

I have the following code: Class definition:

<?php
    class Person{
        var $name;
        public $height;
        protected $socialInsurance = "yes";
        private $pinnNumber = 12345;

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

        public function setName($newName){
            $this->name = $newName;
        }

        public function getName(){
            return $this->name;
        }

        public function sayIt(){
            return $this->pinnNumber;
        }
    }

    class Employee extends Person{
    }

And the part with instances:

<!DOCTYPE html>
<HTML>
    <HEAD>
        <META charset="UTF-8" />
        <TITLE>Public, private and protected variables</TITLE>
    </HEAD>
    <BODY>
        <?php
            require_once("classes/person.php");

            $Stefan = new Person("Stefan Mischook");

            echo("Stefan's full name: " . $Stefan->getName() . ".<BR />");

            echo("Tell me private stuff: " . $Stefan->sayIt() . "<BR />");


            $Jake = new Employee("Jake Hull");

            echo("Jake's full name: " . $Jake->getName() . ".<BR />");

            echo("Tell me private stuff: " . $Jake->sayIt() . "<BR />");

        ?>
    </BODY>
</HTML>

Output:

Stefan's full name: Stefan Mischook.
Tell me private stuff: 12345
Jake's full name: Jake Hull.
Tell me private stuff: 12345 // Here I was expecting an error

As I understand, the private variable is accessible only from it's own class, and the protected variable is accessible also from the classes that extend the class. I have the private variable $pinnNumber. So I expected, that I get an error if I call $Jake->sayIt(). Because $Jake is member of class Employee that extends class Person. And the variable $pinnNumber should be accessible only from class Person, not from the class Employee.

Where is the problem?

Michal Vlasák
  • 247
  • 2
  • 14
  • 1
    Are you accessing `$pinnNumber` in the class employee? You're accessing `sayIt()` which is public thus callable. – Daan Mar 31 '16 at 09:04
  • 1
    `Employee` inherited from `Person`. `Employee` didn't **directly** access the `private` variable. It used a method that comes from parent class (it was inherited), and that parent class is allowed to access the variable. Therefore, it's all good. – Mjh Mar 31 '16 at 09:07
  • @Daan Yes, because $Jack is member of Employee so it takes all properties of Person. All functions, variables, but if the $pinnNumber is private, how is possible that is shown in output. – Michal Vlasák Mar 31 '16 at 09:08
  • 2
    @MichalVlasák No you're not. Because you're calling the public function. Which calls the private property in his own class. – Daan Mar 31 '16 at 09:11
  • If you want to prevent that by purpose, then you have to overwrite the parents method by one implemented in the child class. If that method tries to access the parents private property, then you will indeed get an error. Or, obviously, you could declare the parents method as private or protected. – arkascha Mar 31 '16 at 09:13
  • Interesting: your code works in PHP Version 7 without error and problem. – Michael Mar 31 '16 at 09:22

2 Answers2

4

Actually, that's not how it works. As you didn't extend the sayIt() method, there is no "accessibility problem", there would be one if you did something like this:

<?php
    class Person{
        var $name;
        public $height;
        protected $socialInsurance = "yes";
        private $pinnNumber = 12345;

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

        public function setName($newName){
            $this->name = $newName;
        }

        public function getName(){
            return $this->name;
        }

        public function sayIt(){
            return $this->pinnNumber;
        }
    }

    class Employee extends Person{
        public function sayIt(){
            return $this->pinnNumber;//not accessible from child class
        }
    }
n00dl3
  • 21,213
  • 7
  • 66
  • 76
-1

Protected, public and private are just enviroment scopes, in your case, for a Class. Since your satIt() function is public, you can access the function which then has the correct enviroment scope to access any private or protected variables.

If you tried to do:

$Jake->pinnNumber

Outside the Class, then you'd get the error.

You should research more about Scopes in methods and Classes, then you can move onto anonymous functions ;)

Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • Try to extend `sayIt` in `Employee` with using `this->pinnNumber` and get an error. – u_mulder Mar 31 '16 at 09:07
  • That is already said below, so I just added more to the answer.. so explain the vote down – Jaquarh Mar 31 '16 at 09:08
  • 1
    `sayIt` function has access to `pinnNumber` only because it's not extended in child class, and not because it's public and has access to all variables. – u_mulder Mar 31 '16 at 09:10
  • state the obvious.. `$Jake` is just inherited Person. SO, because what you're saying has **already** been said, I merely just noted that you cannot also access `protected` or `private` variables outside the Classes scope. *slowly claps* – Jaquarh Mar 31 '16 at 09:15
  • Noting that OP can't use private property outside a class is not what OP asks for, So your answer is just a note. – u_mulder Mar 31 '16 at 09:17
  • tl;dr: you're sensitive, its a site - chill out – Jaquarh Mar 31 '16 at 09:37