A PHP class is a template or a blueprint for creating objects. It is actually a user-defined data type having member variables (data) and member functions to operate on that data.
In oop, data is called properties and functions are called methods. Hence, a class consists of properties and methods.
Definition of a Class:
- A class is a template which contains a collection of data members (variables) and code members (functions).
- The main aim of working with classes is abstraction, which means data hiding.
- You can invoke members of the class by creating an instance or object reference to the class. The process of creating an instance of the class is called “Instantiation”.
- A class can have a constructor (s) which is used to initialize the class members.
- The constructor in the class is going to be called at the time of creating an instance of the class. Simply, the constructor in the class loads along with the class object reference.
The following example is to understand how to create a class in PHP. To create a class in PHP, it uses the “class” keyword. In this example, there are two data members, var1 and var2, declared to maintain data and a function, MyFunction () is defined to process data. The data member in the class is to keep data and the code member in the class is called the behavior of the class.
Creating a simple php class:
A class starts with the keyword class, followed by the name of the class, followed by braces. In the braces there are member variables and member functions. The Code for defining a class named as a car is
<?php
class car { }
?>
Adding Data to class:
Now we add data, a variable name to this class.
<?php
class car {
public $var name;
}
?>
Public is called access modifier, related to the visibility and access of this variable, will be discussed in detail shortly.
Adding method/function to our class:
<?php
class car {
public $var name;
public function set_carname($new_carname)
{ $this->name = $new_carname; }
public function get_carname()
{ return $this->name; }
}
?>
Explanation:
We have added two functions or methods to our car class. The first is set_carname () and the second is get_carname (). We added one data variable to our car class, name. Now we are using two functions to access that data variable.
The first function, set_carname ($new_carname), is used to set the name of a car, or put data into the name variable. This function takes one argument, which is, of course, the string car name, which we want to assign to the variable name.
$this is actually a pseudo variable. It is available if a method is called from within a class. $this is in fact a reference to the calling object. We use this to access properties and methods of the current class.
public function set_carname($new_carname)
public access modifier
function keyword used to declare a function
set_carname name of function
$new_carname argument given to function
{ $this->name = $new_carname; }
$this a reference to the calling object, it is a built in variable, pointing current object
name = $new_carname assign the value given as argument in this method to variable name
get_carname() function used to return variable name from this class.
Creating objects; Instantiating a class:
As discussed earlier, a class is a template for an object. However, an object is not created itself. A class has to be instantiated for the creation of an object. We used a new keyword. When a class is instantiated, an instance of the class is created in memory. Keep in mind that here the memory is server memory.
$toyota= new car();
The new keyword is used to create objects from classes, or instantiate a class.
The variable $toyota is a reference or handle to the new object created. We can access this new object with the help of this new variable which was created from the class car.
Another method of creating an object is by gluing
$honda= new car;
i.e. without parenthesis writing the name of class.
Another example of php class:
<?php
class MyClass
{
//declaring class properties
public $val=’some value’;
//declaring a method
Public function showVal()
{
echo $this->val;
}
}
?>
One more example of php class:
<?php
class Car {
/*** defining some properties of car***/
public $name;
public $color;
public $brand;
public $price;
/***constructor function***/
public function_construct(){
echo ‘Car Informatio.<br/>’;
}
/***some public methods***/
public function showPrice(){
echo ‘cost of car’.$this->price.’.<br/>’;
}
}
?>