Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

It seems that I am being able to create a property for a class directly from the instance in PHP?

Tags:

php

In the below program the last statement echo $objb->test; should not output any value. The second last statement should throw an error saying that private member cannot be accessed. However, I am getting output 20 when running the program.

Since PHP is loosely type language is the $objb create a new variable outside the class as well...

<?php
    error_reporting(E_ALL);
    class A {
        public $a;
        private $test;

        protected function sayhello(){
            echo "<p>hello class A</p>";
        }

    }//end of class A

    class B extends A {
        //private $c;
        public function sayhellonew(){
            $this->sayhello();
            echo "<p>hello class B</p>";
        }
    }//end of class B

    $objb = new B();
    $objb->sayhellonew();

    $objb->a = 10;
    echo $objb->a;

    echo "<br>";

    $objb->test = 20;
    echo $objb->test;
    ?>

Output I am getting output 20 why is it working?

like image 918
user3243634 Avatar asked Dec 11 '14 10:12

user3243634


People also ask

How do you declare and access properties of a class in PHP?

Example Explained Here, we declare a static property: $value. Then, we echo the value of the static property by using the class name, double colon (::), and the property name (without creating a class first).

How do you access the properties of an object in PHP?

The most practical approach is simply to cast the object you are interested in back into an array, which will allow you to access the properties: $a = array('123' => '123', '123foo' => '123foo'); $o = (object)$a; $a = (array)$o; echo $o->{'123'}; // error!

What is an instance in PHP?

Definition and Usage. The instanceof keyword is used to check if an object belongs to a class. The comparison returns true if the object is an instance of the class, it returns false if it is not.


3 Answers

Since the $test property is private to A, it is not visible in the scope of B. That means that B can have its own property named $test. When you assign objb->test = 20, that's what you're setting.

The following shows that you're not actually setting the private variable:

class A {
    public $a;
    private $test = 10;

    protected function sayhello(){
        echo "<p>hello class A</p>";
    }
    public function showTestA() {
        echo "Test in A = " . $this->test . '<br>';
    }

}//end of class A

class B extends A {
    //private $c;
    public function sayhellonew(){
        $this->sayhello();
        echo "<p>hello class B</p>";
    }
    public function showTestB() {
        echo "Test in B = " . $this->test . '<br>';
    }
}//end of class B

$objb = new B();
$objb->test = 20;
$objb->showTestA();
$objb->showTestB();

This will display:

Test in A = 10
Test in B = 20
like image 102
Barmar Avatar answered Oct 23 '22 14:10

Barmar


Because, you are overriding the parent class' private property with childs class' public property.

It is valid in PHP.

Try adding a new private property $c in class B and try to override it.

You will get an error: Fatal error: Cannot access private property B::$c

Check it here: http://codepad.org/74IgLqYn

like image 42
Pupil Avatar answered Oct 23 '22 14:10

Pupil


Because its not the test same variable that you have declare in it's parent(A) class. In fact its will treat it as public $test; of class B. Check below you will get the issue.

<?php
    error_reporting(E_ALL);
    class A {
        public $a;
        private $test;

        protected function sayhello(){
            echo "<p>hello class A</p>";
        }

        public function setTest(){
            $this->test = "50";
        }

    }//end of class A

    class B extends A {
        //private $c;
        public function sayhellonew(){
            $this->sayhello();
            echo "<p>hello class B</p>";
        }
    }//end of class B

    $objb = new B();
    $objb->sayhellonew();

    $objb->a = 10;
    echo $objb->a;

    echo "<br>";
    $objb->setTest();
    echo $objb->test;
    ?>

CODEPAD.

like image 38
Rikesh Avatar answered Oct 23 '22 12:10

Rikesh