Tag Archives: OOPS

Object Oriented Concepts

Class: This is a programmer-defined datatype, which includes local functions as well as local data. You can think of a class as a template for making many instances of the same kind (or class) of object.

Object: An individual instance of the data structure defined by a class. You define a class once and then make many objects that belong to it. Objects are also known as instance.

Member Variable: These are the variables defined inside a class. This data will be invisible to the outside of the class and can be accessed via member functions. These variables are called attribute of the object once an object is created.

Member function: These are the function defined inside a class and are used to access object data.

Inheritance: When a class is defined by inheriting existing function of a parent class then it is called inheritance. Here child class will inherit all or few member functions and variables of a parent class.

Parent class: A class that is inherited from by another class. This is also called a base class or super class.

Child Class: A class that inherits from another class. This is also called a subclass or derived class.

Polymorphism: This is an object oriented concept where same function can be used for different purposes. For example function name will remain same but it make take different number of arguments and can do different task.

Overloading: a type of polymorphism in which some or all of operators have different implementations depending on the types of their arguments. Similarly functions can also be overloaded with different implementation.

Data Abstraction: Any representation of data in which the implementation details are hidden (abstracted).

Encapsulation: refers to a concept where we encapsulate all the data and member functions together to form an object.

Constructor: refers to a special type of function which will be called automatically whenever there is an object formation from a class.

Destructors: refers to a special type of function which will be called automatically whenever an object is deleted or goes out of scope.

Abstract Classes

PHP 5 introduces abstract classes and methods. Classes defined as abstract may not be instantiated, and any class that contains at least one abstract method must also be abstract. Methods defined as abstract simply declare the method’s signature – they cannot define the implementation.

When inheriting from an abstract class, all methods marked abstract in the parent’s class declaration must be defined by the child; additionally, these methods must be defined with the same (or a less restricted) visibility. For example, if the abstract method is defined as protected, the function implementation must be defined as either protected or public, but not private. Furthermore the signatures of the methods must match, i.e. the type hints and the number of required arguments must be the same. For example, if the child class defines an optional argument, where the abstract method’s signature does not, there is no conflict in the signature.

Constructor

A is special method of the class that will be automatically invoked when an instance of the class is created is called as constructor.

Constructors are mainly used to initialize private fields of the class while creating an instance for the class.

When you are not creating a constructor in the class, then compiler will automatically create a default constructor in the class that initializes all numeric fields in the class to zero and all string and object fields to null.

Types of Constructors

  • Default Constructor
  • Parameterized Constructor
  • Copy Constructor
  • Static Constructor
  • Private Constructor

Abstract Class vs Interface

1) For abstract class a method must be declared as abstract. Abstract methods doesn’t have any implementation.
For interface all the methods by default are abstract methods only. So one cannot declare variables or concrete methods in interfaces.

2) The Abstract methods can declare with Access modifiers like public, internal, protected. When implementing in subclass these methods must be defined with the same (or a less restricted) visibility.
All methods declared in an interface must be public.

3)Abstract class can contain variables and concrete methods.Interfaces cannot contain variables and concrete methods except constants.

4)A class can Inherit only one Abstract class and Multiple inheritance is not possible for Abstract class.
A class can implement many interfaces and Multiple interface inheritance is possible.

 

Difference Between abstract class and interface PHP

1) For abstract class a method must be declared as abstract. Abstract methods doesn’t have any implementation.
For interface all the methods by default are abstract methods only. So one cannot declare variables or concrete methods in interfaces.

2) The Abstract methods can declare with Access modifiers like public, internal, protected. When implementing in subclass these methods must be defined with the same (or a less restricted) visibility.
All methods declared in an interface must be public.

3)Abstract class can contain variables and concrete methods.Interfaces cannot contain variables and concrete methods except constants.

4)A class can Inherit only one Abstract class and Multiple inheritance is not possible for Abstract class.
A class can implement many interfaces and Multiple interface inheritance is possible.

 

Factory Classes

Factory classes provide an interface for creating families of related objects. Factory classes are useful when the decision of which class to use must be done at run time and cannot be hard coded during development. Factory classes encapsulate the logic needed to decide which subclass to instantiate and so removes this decision from the application, delegating it to the factory.

 

Class

A class is used to specify the form of an object and it combines data representation and methods for manipulating that data into one neat package. The data and functions within a class are called members of the class.

Use classes to encapsulate code and represent objects, and namespaces to avoid symbol name collisions

A. A class can not extend more than one class.

B. A class can not implement more than one class.

C. A class cannot extend more than one interface.

D. A class can implement more than one interface.

Example

<?php
 class a{
 function function_a(){
 echo "Function A";
 }
 }

a::function_a();
 ?>

Data encapsulation

Encapsulation is an Object Oriented Programming concept that binds together the data and functions that manipulate the data, and that keeps both safe from outside interference and misuse. Data encapsulation led to the important OOP concept of data hiding.

Data encapsulation is a mechanism of bundling the data, and the functions that use