Access private properties with for each loop : private « Class « PHP






Access private properties with for each loop

 
<?
    class Person {
            public $FirstName = "B";
            public $MiddleName = "T";
            public $LastName = "M";
            private $Password = "asdfasdf";
            public $Age = 29;
            public $HomeTown = "LA";
            public $FavouriteColor = "Purple";

            public function outputVars( ) {
                    foreach($this as $var => $value) {
                            echo "$var is $value\n";
                    }
            }
    }

    $bill = new Person( );
    $bill->outputVars( );
?>
  
  








Related examples in the same category

1.class member variable access: private
2.Changing the member $a to protected or private
3.Using private and public in Classes
4.If the variable was defined as private static, it would not be possible to access it directly
5.Private and inheritance
6.Private properties are accessible only inside the methods of the class that defined them
7.Protect the class from being misused by accessing the members directly