·  PHP – Syntax – OOP – Classes – Components – Methods – Custom – Scope – Private

 

- Info:

  - Method is declared as private by using keyword private.

  - Public methods can be accessed only from within the class in which they are defined.

  - They even can't be accessed from subclasess (classes that extend this class).

 

- Example:

  - This example shows how to access private method from another method of the same class.

  - Creat PHP Console Application Test.php.

 

    Test.php

    <?php

   

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

      class Person { 

        private $name = "John\n";   

        public  function displayName() { echo   $this->getName(); } 

        private function getName    () { return $this->name;      }           

      }

      

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

      $john = new Person();  //Create Object from Class.

      $john->displayName();  //Function call: Hello from object!

      

    ?>