How to Get the PHP OOP Concept in One Article

0
231
How to Get the PHP OOP Concept in One Article
How to Get the PHP OOP Concept in One Article

Object-Oriented Programming (OOP) or PHP OOP is a popular programming strategy that grouped all the variables and functions of a special topic into a single class accordingly.

Object-Oriented Programming (OOP) is more advanced, effective, developed and usable than procedural programming style.

Because it has-

  • Well organized coding style.
  • OOP provides a modular structure on the code.
  • Reduces the code’s frequency and makes the code reusable.
  • OOP languages come with rich libraries of objects. So you can work fast.
  • Better productivity than procedural programming.

PHP OOP

In the OOP all is an object. Suppose the world is created with moon, sun, earth objects. A car is created with the tire, ring, machine, handle, gear objects.

OOP added with PHP from version 5. It is possible to create many large and complex web applications in PHP using Object Oriented Programming.

Let’s start with an example

The result will be-

In OOP PHP everything declare with class. The class name should name start with capital letter and after the curly bracket. Into the curly bracket function will declare. We know every function by default is ‘public’, but we should write ‘public’ before writing declaring a function in PHP OOP. Here have an issue – Access Modifier. I’ll discuss it later.

Now you need to access the function. So first you need to create an object of the class, I’ve defined with a variable name $classObject, you can use any name. Then use new keyword and then the class name.

Now you can access the ‘function’ by the class object. I’ve done it-

Now let’s create more function into the class.

The result will be-

That means you can create unlimited functions into a ‘class’ and you can access those functions by defining the ‘class object’.

Some important topic on PHP OOP

  • PHP: Class– A Class is the main design (template) for expandable programming code to create an object that combines properties (member variables) and member function (method).
  • PHP: Object – Object is a special instance of a class that can be composed of variables, functions and data structures.
  • PHP: Method – Function of the class is called the method in PHP OOP. A method is a process related to object-classes. It’s created with object information and information-process.
  • PHP: Property – Member’s variable of a class.
  • PHP: Encapsulation – A strategy to combine properties and methods together for creating an object.
  • PHP: Inheritance – Child class can use parent class properties and method to reduce code duplication.
  • PHP: Parent class – All other classes inherit this class. The parent class is called the ‘Super Class’ or the ‘Base Class’.
  • PHP: Child Class – This class inherits all other classes. This class is called sub-class.
  • PHP: Polymorphism – A class has varying functionality while sharing a common interface. Used same name which class methods use in the same purpose.
  • PHP: abstract class and method – This is an object-oriented programming method. In this way, a programmer may hide everything except the relevant information. For example, we use an abstract class to make promises that child class access to certain specific methods of parent classes. But we do not promise what type of code the method will be.
  • PHP: Interface – Parents class promises to child class that the child class can access the abstract methods of parent class for implementation of the method.
  • PHP: public, private and protected property – Properties can be public, private or protected in the PHP OOP. Public means property can be accessed from anywhere, private means can only be accessed from the respective classes and the protected meaning is also accessible from the respective classes and its child classes.
  • PHP: Public, Private and Protected Method – Methods can be public, private or protected. Public means methods can be accessed from anywhere, private means can only be accessed from the respective classes and protected means that access can also be accessed from the respective classes and its child classes.
  • PHP: Construct – PHP’s special type of built-in method. It automatically executes when a class executes. The ‘construct’ method starts with two underscores (__).
  • PHP: Destruct – Its antonym of ‘Construct’. It is automatically called when the object is destroyed.
  • PHP: Type Hinting – When a function declaring, for the argument, it defines the data types (arrays, objects, interfaces, etc.)
  • PHP: Static property and method – Access the class properties and methods without creating new objects. It’s very useful for creating login and session functions.
  • PHP: Object Cloning – Used to create a copy of an object.
  • PHP: spl_autoload_register () – Loads all required classes automatically into the corresponding file. You no need to use ‘include/require’ for every class, it’s load all class automatically.
  • PHP: $ this – This is a special type of variable that points to the same object.
  • PHP: scope Resolution Operator (: 🙂 – Php scope resolution operator (::) is used to access a class’s static, constant and overridden properties or methods.
  • PHP: Magic Method __construct (), __destruct (), __call (), __callStatic (), __get (), __set (), __isset (), __unset (), __sleep (), __wakeup (), __toString (), __invoke ( ), __set_state () and __clone () functions are called PHP magic methods.

Use of $this variable

You can access a class method and object with this variable.

The result will be-

Access modifier

There have three types of access modifier in PHP OOP.
→ Public
→ Private
→ Protected

We’ve already used many times the ‘Public’ modifier. So we’ll now see the ‘private’ modifier. We’re going to use the same example code of ‘$this’ variable.

But I’ve set the property ‘private’. Now it generates a ‘Fatal error:’ and the result will be-

But you can still access the ‘private’ modifier by setting ‘setter’ and ‘getter’. Most of the time I use it in the login/registration system. A session will be set as private, and when then the class will execute, there will have a session or something which I’ll get with ‘getter’ with PHP OOP.

PHP OOP: Inheritance

It’s very interesting to me. Let’s create another example.

Here have a parent class and child class. Main class and sub-class. You can use the main class method and property for your subclass without declaring new method and property.

The result will be-

PHP OOP Abstract class and method

This is an object-oriented programming technique. In this way, a programmer can hide everything except the relevant information. For example, we use an abstract class to make promises that child class access to certain specific methods of parent classes. But we do not promise what type of code the method will be.

More explicitly, we use abstract classes and methods in PHP OOP only when we force some methods of parenting classes to be inherited from the child class.

See an example-

The result will be-

Normally you can access into an abstract class from the child class. So I’ve created another abstract function into the parent class and called it into the child class.

spl_autoload_register()

The ‘spl_autoload_register()’ load all classes dynamically. Suppose in my classes folder have many class like as- ‘class Database{}’, ‘class Format{}’, ‘class Product{}’ etc. Will you call every class one by one? No, you can use the function to call classes automatically. Let’s see an example-

Now I can access into all class method and properties by the class object. I’ve accessed into the Product class method by the code-

PHP OOP: Static Method and Static Property

Main features of the ‘Static Method and Static Property’ is you can access into the class by scope resolution operator (::), without creating an object.

The result will be-

In the article, I’ve tried to give you the basic PHP OOP concept. For advanced knowledge search internet with the topic, I’ve listed here Some important topic on OOP.

If you see any faults and have any question, please put a comment below. I’ll reply. There should not have limitation to learn anything. Share the article as much as possible. – Thanks. 🙂

LEAVE A REPLY

Please enter your comment!
Please enter your name here