·  PHP – Syntax – OOP – Classes

 

- Info:

  - This tutorial shows how to create and use classes.

  - Constructor is method which is called when object of the class is created using keyword new.

  - Destructor is method which is called as soon as there are no other references to a particular object, or during shutdown.

  - Property is variables defined inside a class.

 

- Single File:

  - This example shows how to create and use class within the same file.

  - Creat PHP Console Application Test.php.

 

    Test.php

    <?php

   

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

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

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

      MyClass::displayString();    //Function call: Hello from class!

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

   

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

      class MyClass {

      

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

      

        public function displayVar() {

          echo($this->var);

        }

        

        public function displayString() {

          echo("Hello from classs!\n");

        }   

          

      }

      

    ?>

 

- Class in a separate file:

  - This example shows how to use class contained in another file located in the subdirectory of the script that uses class.

  - Name of the file which contains class doesn't have to be the same as class name but this is advisable.

  - Create PHP Console Application C:\Temp\PHP\Test.php

  - Create C:\Temp\PHP\Vehicles\MyClass.php

 

    Test.php

    <?php

      require_once 'Vehicles\MyClass.php';

 

      $myObject = new MyClass();

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

      MyClass::displayString();    //Function call: Hello from class!

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

    ?>

 

    MyClass.php

    <?php

   

      class MyClass {

      

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

      

        public function displayVar() {

          echo($this->var);

        }

        

        public function displayString() {

          echo("Hello from classs!\n");

        }   

          

      }

      

    ?>


- Class with namespace:

  - This example shows how to use class defined in another file with namespace.

  - Class namespace is independent of class file location.

  - Create PHP Console Application C:\Temp\PHP\Test.php

  - Create C:\Temp\PHP\Vehicles\MyClass.php

 

    Test.php

    <?php

   

      require_once 'Vehicles\MyClass.php';

      

      use ivoronline\testclasses\MyClass;               //Optional alias.

   

      $myObject = new MyClass();                        //Using alias MyClass instead full name

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

      \ivoronline\testclasses\MyClass::displayString(); //Function call: Hello from class!

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

      

    ?>

 

    MyClass.php

    <?php

 

      namespace ivoronline\testclasses;

   

      class MyClass {

      

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

      

        public function displayVar() {

          echo($this->var);

        }

        

        public function displayString() {

          echo("Hello from classs!\n");

        }   

          

      }

      

    ?>