·  PHP – Syntax – OOP – Classes – Components – Properties


- Info:

  - Property is variable defined inside a class.

  - Property can belong to class or object and it can be public or private.

  - Object property can be defined when class is defined or added later.


- static:

  - Keyword static defines class property and only one exists per class, it is therefore shared among class objects.

    These are referenced by using class name MyClass::$classVar or by using self::$classVar inside class method.

  - Missing keyword static indicates object property and one is created for each object.

    These are referenced by using object name $myObject->objVar or by using $this->objVar inside class method.


- public:

  - Keyword public defines that property is accessable both from outside of the class and from the class methods.

    This means that you can use MyClass::$classVar or $myObject->objVar to reference class/object properties.


- private:

  - Keyword private defines that property is NOT accessable from outside of the class but only from the class methods.

  - This means that you CAN'T use MyClass::$classVar or $myObject->objVar to refernece class/object properties.

  - But you can use self::$classVar or $this->objVar to refernece class/object properties from inside clas methods.


- var:

  - Keyword var must be used if none of the above keywords is used to indicate property.

  - This will default to public and non static property.


- Example:

  - Creat PHP Console Application Test.php.





      $myObject = new MyClass();










      $myObject->addedProp="Added Public Object Property";    



      class MyClass {


        public static $classVar = "Hello from class  property!\n"; //static   indicates   class property

        public        $objVar   = "Hello from object property!\n";


        public function displayVar() {

          echo(self::$classVar);       //Calling class  property from function.

          echo($this->objVar);         //Calling object property from function.