·  PHP – Syntax – OOP – Classes – Helper Functions

 

- Info:

  - This tutorial shows helper functions for working with classes.

 

- Example:

  - Creat PHP Console Application Test.php.

 

    Test.php

    <?php

   

      //CLASS.

      class Person {

        var $name = "John";

        var $age  = 50;

        function func1() {}

        function func2() {}

      }

      

      class Soldier extends Person {}

      

      //OBJECT.

      $person  = new Person();

      $soldier = new Soldier();

      

      //FUNCTIONS.

      $bool         = class_exists     ("Person"); //TRUE if class is defined.

      $className    = get_class        ($person);  //Name of the class to which object belongs.

      $methods      = get_class_methods("Person"); //Return indexed array with method names.

      $vars         = get_class_vars   ("Person"); //Return associative array with fields name/values.

      $classes      = get_declared_classes();      //Return indexed array of all declared classes.

      $objvars      = get_object_vars  ($person);  //Return associative array with fields name/values.

      $parentClass  = get_parent_class ($person);  //NOT WORKING.

      interface_exists("MyInterface");

      $objOfClass   = is_a          ($person ,"Person"); //TRUE if object belongs to given class.

      $objOfSubclass= is_subclass_of($soldier,"Person"); //TRUE if object's class extends given class.

      $methodExists = method_exists ($person ,"func1" ); //TRUE if object contains given method. 

   

      //DISPLAY.

      echo("bool     =$bool     \n");

      echo("className=$className\n");

      print_r($methods);

      print_r($vars);

      print_r($classes);

      print_r($objvars);

      //print_r($parentClass);

      print_r($objOfClass);

      print_r($objOfSubclass);

      

    ?>