Tag Archives: OOPS

PHP Encapsulation

The wrapping up of data and methods into a single unit (called class) is known as encapsulation. Encapsulation is a protection mechanism for the data members and methods present inside the class. In the encapsulation technique, we are restricting the data members from access to outside world end-user.

In PHP, encapsulation utilized to make the code more secure and robust. Using encapsulation, we are hiding the real implementation of data from the user and also does not allow anyone to manipulate data members except by calling the desired operation.

 

Differences Between Abstract Class And Interface

  • In abstract classes this is not necessary that every method should be abstract. But in interface every method is abstract.
  • Multiple and multilevel both type of inheritance is possible in interface. But single and multilevel inheritance is possible in abstract classes.
  • Method of php interface must be public only. Method in abstract class in php could be public or protected both.
  • In abstract class you can define as well as declare methods. But in interface you can only defined your methods.

OOP

Object-oriented programming (OOP) is a programming language model organized around objects rather than “actions” and data rather than logic. Historically, a program has been viewed as a logical procedure that takes input data, processes it, and produces output data.

Overriding

Overloading in oop is same as overloading in real word. In real word overloading means assigning extra work to same machine or person. In oop method overloading is same. By process of method overloading you are asking your method to some extra work. Or in some cases we can say some different work also.

Normally method overloading in oop is managed on the basis of the argument passed in function. We can achieve overloading in oop by providing different argument in same function.

Overriding in php is very easy. As we know that overriding is process of modifying the inherited method. So in case of inheritance you only need to create method with same name in your child class which you want to override.

Overloading

As we know that we can not implement overloading by creating 2 function in with same name in class. So to implement overloading in php we will take help of magic method __call. Magic method __call invoked when method called by class object is not available in class. So here we will not create method exactly and will take help of __call method. Now call method will provide us 2 argument, 1st name of the method called and parameter of the function. Now with the help of either switch , case or if else we will implement overloading in php.

Object Oriented Programming

Object-oriented programming (OOP) is a programming paradigm based on the concept of “objects”, which are data structures that contain data, in the form of fields, often known as attributes; and code, in the form of procedures, often known as methods. A distinguishing feature of objects is that an object’s procedures can access and often modify the data fields of the object with which they are associated (objects have a notion of “this”). In OO programming, computer programs are designed by making them out of objects that interact with one another. There is significant diversity in object-oriented programming, but most popular languages are class-based, meaning that objects are instances of classes, which typically also determines their type.

stdClass

stdClass is Generic empty class in PHP OR stdClass is universal base class in PHP.

stdClass is used to create anonymous objects with properties.

When we need to create an object without having new class. then we used stdClass class which is inbuilt in PHP.
After create object, we can add properties.

<?php
$object=new stdClass();
$object->name=’PHPCodez’;
echo $object->name;
?>

Friend Function

A friend function is a function that is not a member of a class but has access to the class’s private and protected members. Friend functions are not considered class members; they are normal external functions that are given special access privileges.

Accessors

Accessors are instructions that generally give a property of method permission to be accessible in different ways. There are three types:

public: This property or permission is accessible from anywhere in the application.

protected: This property or permission is accessible only from a class that extends the parent class.

private: This property or permission is accessible only within the current class you are working with.

The really neat thing about access is that it protects your code should other developers be working on the same project. If you don’t want them to be able to touch something, don’t give him access to it.