&, obj2, syntax, coding"/>

PHP 5 Class And Object

PHP 5 Class and object

One of the biggest nightmares for developers is maintaining code where data is declared over and over again, meaning any changes to the program become an infinitely job. Developers introduces new programming syntax called concepts object oriented programming (OOP).

OOP is a style of coding where functions and methods are grouped into classes, if a piece of information changes in your program, usually only one change is required to update the whole code.

Classes & Objects

<?php
class MyClass{
public function sqrt($val){
return(sqrt($val));
}
}
?>

The power of OOP is we are able to use multiple instances of the same class.

<?php
$obj = new MyClass();
$x = 5;
echo 'The Square of '.$x.' is '.$obj->sqrt($x);

------


$obj2 = new MyClass();
$y = 5;
echo 'The Square of '.$y.' is '.$obj2->sqrt($y);
?>
Categories: Tutorials