Properties of a PHP Class

Class member variables are called “properties”.They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration.In order to maintain backward compatibility with PHP 4, PHP 5 will still accept the use of the keyword var in property declarations instead of (or in addition to) public, protected, or private.

Class properties must be defined as public, private, or protected. If declared using var, the property will be defined as public.

Property Visibility

Class members declared public can be accessed everywhere. Members declared protected can be accessed only within the class itself and by inherited and parent classes. Members declared as private may only be accessed by the class that defines the member.

Property declarations
<?php

class MyClass
{
public $name = ‘Kiran’;
protected $age = 24;
private $qualification = ‘MCA’;

function printHello()
{
echo $this->name;
echo $this->age;
echo $this->qualification;
}
}

$obj = new MyClass();
echo $obj->name; // Kiran
echo $obj->age; // Fatal Error
echo $obj->qualification; // Fatal Error

$obj->printHello(); // Shows Public, Protected and Private

?>

OUTPUT :
Kiran
Fatal error: Cannot access protected property MyClass::$age on line 17