PHP As An Object Oriented Programming Part-2

Class Method:- The function which is declared in a class is called Method.

The method can be Public, Private, or protected.

Valid method name can start from underscore or letter and follow by any letter, number, or underscore.

Default method property will be public.

method body will be enclose with curly braces.

we can access any method and properties of the current instance using $this like $this->method name


class check_number{

public $num;

function oddeven($num){


$n = ($num % 2);

if($n == 0){

print “$num is an even number”;


print “$num is an odd number”;



print “$num is not a number.”;




$numchk = new check_number;

$numchk ->num  = 13;

echo  $numchk-> oddeven(13);


Output will be 13 is an odd number.

Constructor Function:- It is special function. It automatically execute when object of class will create or instantiated.

PHP use magic function __construct for declare constructor method.

function __construct( $name1, $name2 ) {

$this->state = $name1;

$this->city = $name2;



we can pass as many as arguments we want. 

Destructor Function:- PHP provide magical function __destruct()

It calls or define for destruct or stop the script and exit. In other word,  We can release all the resource.

Scope Resolution (::) Operator:- Scope resolution operator is use for access static, constant, and Overridden properties or methods of a class. It is a token and also called Paamayim Nekudotayim and simply double colon.

The keyword self, parent and static keyword is used to access property or method from inside the class definition.

class A {

static x=’red’;

const y = ‘blue’;

static function z(){

echo self::y;



echo A::y;

$clsnam = “A”;

echo $clsnam::y;

$B = new A();


echo $b::y;

Share with:


Leave a Reply

Connect with: