PHP Properties Basics

PHP Properties Basics

The code below is about as simple as it gets to show how public, protected and private properties can be accessed.

As you can see, the parent class 'tag' has a public, protected and private property. After the $test object is instantiated with the line $test = new Form(); the constructor runs. As you can see, it only outputs the public and protected properties. On the bottom of the page, only the public property '$test->name' will print.

class Tag
{

    public $name = "Peter";
    protected $name2 = "Paul";
    private $name3 = "Mary";


}

class Form extends Tag
{

    function __construct()
    {
        echo $this->name;
        echo $this->name2;
        echo $this->name3;
    }


}

$test = new Form();
echo "<br>";

echo $test->name. " \n";
echo $test->name2. " \n";
echo $test->name3. " \n";