PHP Object Oriented Programming (OOP) | Introduction to object oriented programming in PHP

Object Oriented Programming (OOP)

Object Oriented Programming in PHP


Introspection:

  • It is the ability of a program to examine an object’s characteristics, such as its name, parent class, properties and methods.
  • Introspection, allow you to
  • Obtain name of class to which on objects belongs and properties and methods
  • Write generic debugger, serializes, profile etc.

Examining Classes:

The introspective functions provide by PHP


1.  class_exists() : To determine whether a class exits, takes in a string and return a Boolean value.
      Syntax: $yes_no=class_exists(classname);
 
2. get_declared_classes () : Return an array of defined classes and you can verify the class name in the     returned array :
    syntax : $classes = get_declared_classes();
 
3. get_class_methods() : You can get the methods and properties that exist in a class. This function taks     a class name and returns an array:
    syntax : $methods = get_class_methods(classname);
 
4. get_class_vars() : returns only properties that have default values
    syntax : $properties = get_class_vars(classname);
 
5. get_class() : To get the class to which an object belongs, first check if it is an object using the                 is_object function, then get the class with the get_class() function
     syntax : $yes_no = is_object(var);
     $classname = get_class(object);
 
6. get_parent_class() : Retrive parent class name for object or class.
    Syntax : superclass = get_class(object);
 
7. get_object _vars() : Returns an array of properties that are set in an object.
    Syntax : $array = get_object_vars(object);
 
8. method_exists() : Before calling a method on an object, you can check that is exists using the function :
    syntax : $yes_no = method_exists(object, method);


Serialization :

  • It is conversion of any PHP value into a byte stream representation is called as serialization.
  • This byte stream can be stared in a file

Syntax : string serialize(value);

  • When an object is serialized, it will save all variable in an object also it store the name of class.
  • The methods in an object not saved.
  • To convert byte stream representation back to PHP value, used unserialize(string) function.

Syntax : value unserialize(string);

  • The sleep() method is called on an object just before serialization. It clean up the object .
  • The wakeup() method is called on an object immediately after an object is created form a byte stream.

 

Inheritance :

  • It is the capability of a class to drivel inherit properties and methods form another class is called inheritance.
  • A new class can inherit the properties of methods form the old class.
  • The old class is base class of the new class is derive class.
  • The derived class has its own variables and methods plus variables and methods form the base class.
  • The extends keyword is used for inheritance
  • The public property or method of base class will be public in the derived class
  • The protected member will protected after inheritance
  • The private property will not be inherited but it can be manipulated through public inherited method of base class.

Syntax:

class derived_classname extends base_classname

           {

                                //properties

                                //methods

}

 Example :

class employee

{

                private $emp-code,$emp_name;

}

Class employeesalary extends employee

{

                private $basic_pay, $earning, $deduction;

}

 

Final Method:

  • When method declared using final keyword in superclass/parent class, then subclass/childclass can not override that method.
  • Properties and constants can not be declared final, only classes and methods may be declared as final.

Abstract class and abstract method :

  • Abstraction is a way of hiding information. There should be at least one method that must be declared but not defined.
  • Any class that contain at least one abstract method must also be declared as abstract.

syntax:

abstract class classname {

                //abstract method declaration

                //non-abstract method definition

}

 

Characteristics of abstract class :

  • abstract keyword written before class for it to be an abstract class.
  • It implements all methods of an abstract class can be instantiated.
  • If the derived class which inherit the abstract class doesn’t define all abstract methods then the derived class can’t be instantiated.

Interface :

  • Is a class with no data members of contain only member function
  • Interface declare only methods of not any variables.
  • The method implementation depends upon the class which implement them.

 

Characteristics of an interface :

  • Similar to class except it can not contain code.
  • It define method name and argument but net contains of method
  • All methods declared are public.
  • Any class implementing an interface must implement all methods define by the interface.
  • A class implement multiple interface
  • An interface declare using “Interface” keyword.
  • Interface can not maintain non-abstract methods.

 

Encapsulation :

  • The ability to hide the implementation is knows as encapsulation
  • It used to protection of a class.
  • The variable or data of a class or hidden form any other class and can be accessed only member function of that class.
  • This allow a class to change it’s internal implementation without affecting overall functionality.
  • The either classes don’t have direct access to the properties.
  • Generally all data members of class should be declared private.
  • The protected members can be accessed by method which are declared as public as well as derived class’s public methods.


Advantages of Encapsulation :

  • You can change implementation details at any time without affecting code that user class.
  • Outside class can’t modify property values of an object built from your class without your knowledge.

PHP Object Oriented Programming (OOP) | Introduction to object oriented programming in PHP PHP Object Oriented Programming (OOP)  | Introduction to object oriented programming in PHP Reviewed by technical_saurabh on April 29, 2021 Rating: 5

No comments:

Powered by Blogger.