Classes in object-oriented PHP combine variables and functions into groups called classes. Objects are instances of classes that use the class. Some advantages of using object-oriented PHP include grouping code, increased clarity, inheritance of classes, and automatic initialization of functions through constructors and destructors. A class syntax example is provided, as is an example class for a car that has a type property and getType method. Database connection using a class is also demonstrated.
2. Object Oriented PHP
Class is the combination of variables and functions.
Combined together into one group.
Object is the example of the class who actually uses the class.
3. Advantage of Using Object Oriented PHP
1. Grouping of code.
2. More Clarity.
3. Classes can be inherited.
4. Automatically initialization of function.
i.e. Constructor and Destructors
6. Example :-
class Car
{
var $type;
function __construct($type)
{
$this->type = $type;
}
function getType()
{
return $this->type;
}
}
$myCar = new Car("sports);
echo "Car type: " . $myCar->getType();
7. Database Connection Using Class
class test
{
function __construct()
{
$this->c=mysql_connect(HOST,UNAME,PASS)or die("error in connection");
mysql_select_db(DB,$this->c);
}
function login($user,$paas )
{
.......code
}}