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?

6
  • 1
    Are you accessing $pinnNumber in the class employee? You're accessing sayIt() which is public thus callable. Commented Mar 31, 2016 at 9: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. Commented Mar 31, 2016 at 9: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. Commented Mar 31, 2016 at 9:08
  • 2
    @MichalVlasák No you're not. Because you're calling the public function. Which calls the private property in his own class. Commented Mar 31, 2016 at 9: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. Commented Mar 31, 2016 at 9:13

2 Answers 2

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
        }
    }
Sign up to request clarification or add additional context in comments.

Comments

-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 ;)

6 Comments

Try to extend sayIt in Employee with using this->pinnNumber and get an error.
That is already said below, so I just added more to the answer.. so explain the vote down
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.
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
Noting that OP can't use private property outside a class is not what OP asks for, So your answer is just a note.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.