·  PHP – Syntax – OOP – Classes – Components – Fields – Invoking

 

- Info:

  - Fields are referenced by using -> operator and, unlike variables, are not prefaced with a dollar sign.

  - When you refer to a field from within the class in which it is defined, you use the $this keyword.

 

- Example:

  - This example shows how to define Class with two fields and one function.

  - Creat PHP Console Application Test.php.

 

    Test.php

    <?php

   

      //USE CLASS.----------------------------------------------------------

      $myObject = new MyClass();   //Create Object from Class.

      $myObject->displayString();  //Function call: Hello from object!

      echo($myObject->var);        //Variable call: Hello from object!

   

      //DEFINE CLASS.-------------------------------------------------------

      class MyClass {

      

        public  $var = "Hello from object!\n";

        private $var2;

        

        public function displayString() {

          echo($this->var);

        }   

          

      }

      

    ?>