php

 

PHP WITH OOPS CONCEPT

Object-oriented programming is a programming model organized around Object rather than the actions and data rather than logic.

Class:

A class is an entity that determines how an object will behave and what the object will contain. In other words, it is a blueprint or a set of instruction to build a specific type of object.

In PHP, declare a class using the class keyword, followed by the name of the class and a set of curly braces ({}).

PHP WITH OOPS CONCEPT

This is the blueprint of the construction work that is class, and the houses and apartments made by this blueprint are the objects.

Syntax to Create Class in PHP

  1. <?php  
  2. class MyClass  
  3.     {  
  4.         // Class properties and methods go here  
  5.     }  
  6. ?>  

Important note:

In PHP, to see the contents of the class, use var_dump(). The var_dump() function is used to display the structured information (type and value) about one or more variables.

Syntax:

  1. var_dump($obj);  

Object:

A class defines an individual instance of the data structure. We define a class once and then make many objects that belong to it. Objects are also known as an instance.

An object is something that can perform a set of related activities.

Syntax:

  1. <?php  
  2. class MyClass  
  3. {  
  4.         // Class properties and methods go here  
  5. }  
  6. $obj = new MyClass;  
  7. var_dump($obj);  
  8. ?>  

Example of class and object:

  1. <?php  
  2. class demo  
  3. {  
  4.         private $a"hello javatpoint";  
  5.         public function display()  
  6.         {  
  7.         echo $this->a;  
  8.         }  
  9. }  
  10. $obj = new demo();  
  11.     $obj->display();  
  12. ?>

  13. ABSTRACT CLASS

    An abstract class is a mix between an interface and a class. It can be define functionality as well as interface.

    • Classes extending an abstract class must implement all of the abstract methods defined in the abstract class.
    • An abstract class is declared the same way as classes with the addition of the 'abstract' keyword.

    SYNTAX:

    1. abstract class MyAbstract  
    2. {  
    3.     //Methods  
    4. }  
    5. //And is attached to a class using the extends keyword.  
    6. class Myclass extends MyAbstract  
    7. {  
    8.     //class methods  
    9. }  

    Example 1

    1. <?php  
    2. abstract class a  
    3. {  
    4. abstract public function dis1();  
    5. abstract public function dis2();  
    6. }  
    7. class b extends a  
    8. {  
    9. public function dis1()  
    10.     {  
    11.         echo "javatpoint";  
    12.     }  
    13.     public function dis2()  
    14.     {  
    15.         echo "SSSIT";     
    16.     }  
    17. }  
    18. $obj = new b();  
    19. $obj->dis1();  
    20. $obj->dis2();  
    21. ?>  

    Access Specifiers in PHP

    There are 3 types of Access Specifiers available in PHP, Public, Private and Protected.

    Public - class members with this access modifier will be publicly accessible from anywhere, even from outside the scope of the class.

    Private - class members with this keyword will be accessed within the class itself. It protects members from outside class access with the reference of the class instance.

    Protected - same as private, except by allowing subclasses to access protected superclass members.

    EXAMPLE 1: Public

    1. <?php  
    2. class demo  
    3. {  
    4. public $name="Ajeet";  
    5. functiondisp()  
    6. {  
    7. echo $this->name."<br/>";  
    8. }  
    9. }  
    10. class child extends demo  
    11. {  
    12. function show()  
    13. {  
    14. echo $this->name;  
    15. }  
    16. }     
    17. $objnew child;  
    18. echo $obj->name."<br/>";     
    19. $obj->disp();  
    20. $obj->show();  
      








Comments