PHP Object Oriented Programming Part-1 (PHP OOP Basics)

Introduction to Object Oriented Programming:

PHP Object Oriented Programming Part (PHP OOP Basics)

To understand object oriented programming, we first need to know what is object? And what is class?

What is Object?

Everything we see around us is object. Suppose you look at yourself and your surroundings, your coat, your pants, mobile, tables, chairs, computers all these are objects. Even you and I myself are an object

What is Class?

Now we know about object. Let’s know about class. A class is the replica of an object or the blueprint of an object. Suppose we want to make a chair. At the beginning, we can not make a chair without any thought. We plan for it – what will be the chair looks like, how much will it be in length, width and height etc. These plans are what we write somewhere as document. Our document is actually a class. To explain a little bit more, we can say that the name of the object is determined by the name of the class. The object created by a class is called an instance of that class.

What is Object Oriented Programming?

Now the question is, what is Object Oriented Programming? Object Oriented Programming is a computer programming which is defined by using the concept of class and object is called Object Oriented Programming. Object Orient theory is founded by three concepts:

  • Inheritance– Inheritance is a method of sharing some of the functionality and properties from one class to another class. Or Inheritance is a process in which, any class or object attains the characteristics of any other class or object. It is easier to say that, The Inheritance is a process where a child class inherits some of the features from its parent class.
  • Encapsulation– That means, all things remain encapsulated. Much like capsule medicine. Just like a capsule is wrapped inside a vein. Similarly, in Object Oriented Programming, everything is covered with a cover of a class.
  • Polymorphism– Multiformity. That is, an object can take many forms at different times and can be used in many ways.
READ  PHP Object Oriented Programming Part-3( PHP OOP Method Chaining)

What are the things to know before learning Object Oriented Programming in PHP?

Before learning Object Oriented Programming in PHP, what are the things to know?

  • Parent Class: When a class inherits from another class, then the class from which the new class is created, is called parent class. Parent class is called base class or super class.
  • Child Class: When a class is inherited from another class, it is called child class. The child class is called subclass or derived class
  • Visibility/Access modifier: Any property, constant or method in class can be accessed with visibility. Visibility is usually declared in class by constant, property, method before the announcement.

    Three types of visibility in PHP:

    • private: If the constant, property, method in class is to restrict the usage of only the same class or class, then those constant, property, methods are to be declared private.
    • protected: If constant, property, method in class is to restrict the usage of only the same class or class itself and its child class, then those constant, property, methods are to be declared protected.
    • public: If you want constant, property, methods in class, use the same class or class itself, child class and class outside, then those constant, property, methods are to be declared public.
  • Property: Variables in the class in PHP are called properties. Properties are also called “attributes” or “fields”. Property in PHP is to define with any one visibility: public, private, protected.
  • Method: Functions in class in PHP are called method. The methods in PHP are to declare with any one of the visibility: public, private, protected.
  • static keyword: If you want to give access to any property or method without any instance or object, or if you want to access it directly by class, then static keyword is used.
  • “::” scope resolution operator: To use any static property, static method and constant in the class in PHP, use “:: scope resolution operator” if you want to use it inside or outside the class.
  • “$this” pseudo-variable: “$this” pseudo-variable originally contained the object of the current class. If you want to use any non-static property in PHP and non-static method within the class, you need to use “$this” pseudo-variable.

Leave a Reply

Your email address will not be published. Required fields are marked *