Access visibility of public, protected, and private Properties/Members in PHP.

ahsan's weblogs

A key paradigm in OOP is encapsulation and access protection of object properties (also referred to as member variables). Most common OO languages have three main access restriction keywords: public, protected, and private. When defining a class member in the class definition, the developer needs to specify one of these three access modifiers before declaring the member itself. In case you are familiar with PHP 3 or 4’s object model, all class members were defined with the var keyword, which is equivalent to public in PHP 5. var has been kept for backward compatibility, but it is deprecated, thus, you are encouraged to convert your scripts to the new keywords:

class MyClass {

public $publicMember = “Public member”;

protected $protectedMember = “Protected member”;

private $privateMember = “Private member”;

function myMethod(){

// …

}

}

$obj = new MyClass();

This example will be built upon to demonstrate the use of these…

View original post 421 more words

Leave a comment

Filed under Uncategorized

Leave a comment