PHP Objects

PHP: Object Oriented Programming (OOP) php objects

 

A class is a ‘blueprint’ for an object, just like a function that you’d declare and use, a class merely describes a type of object. Before you can do useful logical work with it, you need to create an object (in pure OOP terms instance of a class) using a process called instantiation.
Once you have an object, you can call the methods that are defined in the class.

 

Class can also be defined as a ‘Black Box’ from where you can create objects and access its attributes or properties (variables) and methods (functions).

 

OOP is very useful if you have a large number of objects of the same type in a system in that case you just have to create the objects and then manipulate them all in the same way, without rewriting any code. Moreover, an object is able to maintain it’s state (variable values and such related) throughout the execution of the program.

 

Here is small example of class and object.

[php]
<?php
class Scriptarticle {

public $post_date, $author;

public function Post() {
echo "You are in post method";
}

public function Page() {
echo "You are in page method";
}

public function Comment() {
// we can write the logic related to comment here.
echo "You are in comment method";
}
}

// class intstantiated
$objectOfScriptarticle = new Scriptarticle();

// varible initilisation
$objectOfScriptarticle->author =’Mahesh’;

// use of class method
$objectOfScriptarticle->Post();
?>
[/php]

You can also know weather the variable you are using is an object of a class or not by the below function.

[php]
bool is_object ( mixed $var )
[/php]