Loading

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

Example:-

class check_number{

public $num;

function oddeven($num){

if(is_int($num)===true){

$n = ($num % 2);

if($n == 0){

print “$num is an even number”;

}else{

print “$num is an odd number”;

}

}else{

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();

$B->z();

echo $b::y;

Share with:


7+

Leave a Reply

Connect with:




z35W7z4v9z8w