Class and Object in PHP

Introduction

A Class is a blueprint for an object which informs the compiler what methods and properties will be in an object, instances of the class. Basic class definitions begin with the keyword "class" followed by a class name. The class is a template for a real-world object to be used in a program. For example, a programmer can create a car class which represents a car. This class can contain the properties of a car (color, model, year, etc.) and methods (functions) that specify what the car does (drive, reverse, stop, etc.).

Creating a Class

To create a class, use the "class" keyword, followed by the class name, followed by an opening brace and a closing brace. All the specifics and logistics of the class go between the braces. Class variables are known as properties.

Syntax

class person
 {                                 
 } 

Example1

<?php

classperson

{

 var $name;

 var $job;

}

?>

Example2

<?php

 classStyle

 {

 var $text

 var $alink;

 var $vlink;

 var $link;

 var $bgcol;

 var $face;

 var $size;

 var $align;

 var $valign;

 }

?>

This example uses the Person class and creates two variables for it. The first is $name, a variable which will store the name of the person. The second is $job, a variable which will store the person's job.

Note 

When declaring class variables, they must be declared with the var keyword. Without the var keyword variables are not declared.

Creating Objects


An object is the copied and active form of a class. It's an instance of its class, copied into memory, and can use the properties and methods defined in the class.

Syntax

$object_Name = new Class_Name();

<?php

classTest

{

    staticpublicfunctiongetNew()

    {

        returnnewstatic;

    }

}

classChildextendsTest

{}

$obj1=newTest();

$obj2=new $obj1;

var_dump($obj1 !== $obj2);

$obj3=Test::getNew();

var_dump($obj3instanceofTest);

$obj4=Child::getNew();

var_dump($obj4instanceofChild);

?>

Output

class-and-object.jpg
Creating Function in Class

 

Class functions are just like regular functions. Class functions are known as methods and they can be used to either retrieve data or manipulate data. We will be referring to class functions as functions or methods.

 

Example1

<?php

classBook

{

  var $Name;

  var $job;

  functionsetjob($job){

  $job=$job

}

 functiongetjob()

{

 return $job;

}

 functionsetname($name)

{

 $name=$name;

}

 functiongetname()

{

 return $name;

}

}

?>

 

Example2

<?php

classStyle

{

    functionStyle ($text="#000000",$alink="#AA00AA",

    $vlink="#AA00AA",$link="#3333FF",

    $bgcol="#999999",$face="Book Antiqua",$size=3,$align="CENTER",$valign="TOP")

{

    $this->text=$text;

    $this->alink=$alink;

    $this->vlink=$vlink;

    $this->link=$link;  

    $this->bgcol=$bgcol;

    $this->face=$face;

    $this->size=$size;

    $this->align=$align;

    $this->valign=$valign;

}

}

      ?>

Up Next
    Ebook Download
    View all
    Learn
    View all