articles

Home / DeveloperSection / Articles / Class and Object in PHP

Class and Object in PHP

Anonymous User2972 17-Jun-2016

Object and class are key part of object oriented programming in PHP. Let’s understand what are classes and objects.

Object in PHP

 Anything in this real world is an object. Look around and you will find lots of objects like mobile, car, laptop, etc. These objects have two things properties and behaviors. If you can find properties and behaviors of object then it will be very easy for you to understand object oriented programming.

Let’s take an example of car. Your car has property like color, brand, etc and behavior it can go forward and backward.

Class in PHP

A class is a blueprint of data. Class represents all the properties and behavior of an abject. For example, before building a house you will require a blueprint which defines the shape of the house, how rooms to be made and how they should be connected all these things are planned out, even though the house doesn’t exist. In programming we call blueprint as class.

  Rules for naming classes:

·    A class name must begin with a letter & can be followed by a sequence of letters (A-Z), underscore (_), and digits (0-9).

·    Special Characters such as? ( ) @ # $ % $ cannot be used in the class name.

·     A class name should not be same as reserved keywords.

A class is not an object but are templates used to generate objects. Concept of class was introduced in PHP 4 but the major concept of class was introduced in version 5(known as PHP 5). You create class using keyword class.

Step 1: Create a class

We start by defining our class Myclass

<?php
class Myclass {
}
?>

 

Now we will define our class using constructor. A constructor is a function which is called while creating a new object. In PHP 5 this is done by two ways: Creating a public function with the same name of the class or by creating a function called “__construct()”. This is how it works:

Step 2: creating a class constructor- (using the same name as the name of the class).

<?php
class My {
    public function Myclass() {
        echo "My first class is created successfully.";
    }
}
$obj = new Myclass(); // prints "My first class is created successfully."
?>
(using __construct function)

<?php

class Myclass {
    public __construct() {
        echo "My first class is created successfully.";
    }
}
$obj = new Myclass(); // prints "My first class is created successfully."
?> 
Constructor of Classes and Objects
Default Constructor:

Constructor is a function which is defined in your PHP class. This function is automatically called when you create object of a class. As soon as you write $class = new Myclass() your constructor function will be executed. In PHP4 you can create constructor function by creating a public function with the same name of the class. From PHP 5 you can create constructor by defining __construct() function(This is known as magic function.) Please go through the examples:

PHP 4 constructor (also work in PHP5)

<?php
class Human
{
    public function Human ()
    {
        echo " Hello World!!"; // display Hello World
    }
}
$obj =new Human();
?>

 PHP5 Constructor

<?php
class Human
{
    public function __construct()
    {
        echo " Hello World!!"; // display Hello World
    }
}
$obj=new Human();
?>

 

NOTE-As you can see from above examples, the constructor looks similar to normal function. In PHP, functions starts with two underscores at prefix (__construct())are called as magic function.

Parameterized Constructor:

The advantage of parameterized constructor is the ability to pass parameters which is used as initializing member variables. These parameters are set inside the constructor, which is closer to the real cases.

<?php
class Human
{
    public function __construct($name)
    {
        $this->name = $name;
    }
}
$human = new Human ("Hello World!!"); // Object is created
echo $human ->name; // display Hello World!!
?>

If you have created parameterized constructor then you need to pass values on the time of object creation. As for above example, $human = new Human ("Hello World!!"). If you will not pass the value then PHP will throw error.



 


php php  class 
Updated 28-Nov-2017
I am a content writter !

Leave Comment

Comments

Liked By