vasin
vasin

Reputation: 470

PHP: strange behavior with traits

The following code output 1, but why? The property is private and child class shouldn't have an access to it.

<?php
trait PropertiesTrait {
    private  $same = true;
}

class PropertiesExample {
    use PropertiesTrait;
    public function foo(){
        echo $this->same;
    }
}
(new PropertiesExample())->foo();
?>

Upvotes: 0

Views: 80

Answers (1)

&#193;lvaro Gonz&#225;lez
&#193;lvaro Gonz&#225;lez

Reputation: 146430

  1. You don't have child classes. You only have PropertiesExample.

  2. You are not accessing the property from outside. You have a getter method.

Upvotes: 3

Related Questions