What Is The Concept Of OOP In PHP

oop in php in a sequence.png

OOP in PHP is a concept of doing code. There is two concept to go forward with any project in PHP. First is procedural concept and the second is Object Oriented Programming so almost it’s depends on choice. But initially if you are new to PHP or in programming though, you should have a look at procedural too.

But on the other hand, OOP ( object oriented programming ) is much faster and bit easier than procedural programming.

OOP

In OOP you create dry code that means Don’t Repeat Yourself. Basically you can use the written code very quickly and in very formatted way. For example, If you have a class in OOP then you create multiple objects to that class. And by using the same thing you can easily fetch data through these objects in other files also. It feels very easy and smart way to do things.

Procedural

In procedural you do things differently and there is less options to make your code DRY. It’s possible when you do use functions, when you are repeating a code multiple time. But the difference is when you do make a function then you will be creating a way to do things in structure. But in OOP it’s already provided by PHP when you use Classes and Objects.

What we will learn

In this, we will cover some main aspects of Object Oriented Programming. In which we will use

  1. Class
  2. Objects
  3. Constructor
  4. Destructor
  5. Properties and Methods

and much more.

NOTE :- If you are new into OOPs and over thinking about this concept as I did. Then think once there’s no growth for over thinkers only doers can do it. And there’s nothing that a person cannot learn.

What is Class and Objects In OOP In PHP

Class

Basically, When you do work with object oriented programming then these two are the main hero’s of OOP concept. A class is a template for Objects. And we create class using class keyword. For example, Let’s create a class with the name of programming.

<?php
class programming {} //way to create class

A class is basically a template in which we can create objects.

Objects

Object is a part of class. Object is an instance of a class. We can create multiple objects from a single class with different values each time. For example as we have defined a class above so, now we can create an object from the programming class.

We initialize object with new keyword.

$value = new Programming(); //creating object from a class

What is Constructor

Constructor is function in a class when you do create it, It initialize the object’s properties.

  1. We create Constructor using __ double underscore <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">__construct()</mark>
  2. Constructor initialize properties automatically when you create an object from Class

Below code is showing that when you do create an object from class Programming it will call the __construct() function automatically.

<?php
    class Programming
    {
        function __construct()
        {
            echo  'Default name of Language is PHP';
        }
    }

    $name = new Programming; //created object

If you will have any other method with any other name It will not call that function. Then you have to create the object for that case.

For example : Now we have another method called <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">get_language()</mark> so, to call this method we have to initialize this particularly.

    <?php
    class Programming
    {
        function __construct()
        {
            echo  'Default name of Language is PHP <br>';
        }
        function get_language()
        {
            echo 'PHP';
        }
    }

    $name = new Programming;

    $language = $name->get_language();


//Output

Default name Of Language is PHP
PHP

Now, It will call both of the methods.

What Is Destructor

Destructor is totally opposite to the Constructor because constructor calls by default on initialization of an object. and on the other hand __destruct() call itself at the end of the script and also we can say when script stopped execution.

Here’s the good example of destruct and Construct.

  <?php

    class Programming
    {
        function __construct()
        {
            echo  'Default name of Language is PHP <br>';
        }
        function first()
        {
            echo 'language will be HTML  <br>';
        }
        function second()
        {
            echo 'language will be CSS  <br>';
        }
        function third()
        {
            echo 'language will be JS  <br>';
        }
        function __destruct()
        {
            echo 'language will be PHP';
        }
    }

    $name = new Programming;
    $name->first();
    $name->second();
    $name->third();

//Output

Default name of Language is PHP
language will be HTML
language will be CSS
language will be JS
language will be PHP //Destructor call itself at the end of the script

Both methods helps in to execute code by default and sometimes we need to execute code in starting and in the end. So, Obviously it helps to reduce the code. Definitely follow the DRY concept.

What is Properties and Methods

Properties : Properties are the variables that we do use in a class. We can use these properties to pass in the method also.

  <?php
    class Programming
    {

//properties
        public $name;
        public $advantages;
    }

Method : Creating a function in a class called method. First() is a method below.

 <?php
    class Programming
    {

//method first()
        function first()
        {
            echo 'language will be HTML  <br>';
        }
    }

Access Modifiers

As above we have learned about properties and Methods. And you might have noticed that I have use <mark style="background-color:rgba(0, 0, 0, 0)" class="has-inline-color has-vivid-red-color">public</mark> keyword with property. So, this is a access modifier.

When we use Methods and Properties an access modifier decides where we can use them. Three types of access modifier are out there that you can use.

  1. Public can be used with properties and methods, can be accessed anywhere.
  2. Private can be used with properties and methods and can only be used in the class where it has defined.
  3. Protected can be used with properties and methods and can only be accessed within the class or by the classes which are extended to this class.

NOTE : If you will try to use private properties or methods out of the comfort it will throw a fatal error. Do practical than just reading and watching.

   <?php
    class Programming
    {
        public $name;
        private $advantages;
        protected $reviews;

        public function name() //public
        {
        }
        private function githubstars() //private
        {
        }
        protected function user_reviews() //protected
        {
        }
    }

Notice : We will include next part soon.

Larachamp

1 thought on “What Is The Concept Of OOP In PHP”

Leave a Comment