Class in PHP:
PHP is a server-side scripting language, it is used in web development but also used in general purpose programming language. A class represents an object, with associated local variables and local methods. Also class is a User-Defined data type. If you are a beginner in PHP, you must know the basic knowledge of OOPs otherwise you can join basic C/CPP language course to clear OOPs concepts.
This article represents some concepts and a code example on how to write a PHP class.
Following are some of the key points described in this article:
- Creating a Class
- How to Create a variable in PHP
- Creating a function in Class
- Instantiating a Class
- A simple example of Class
1- Creating a Class
A class is the blueprint of objects. Creating class in PHP language is so easy. You can create a class with the help of using ‘Class’ keyword. The name of the class will be general string without space. The class starts from ‘{‘ (Opening brace) and End with ‘}’ (Closing Brace).
Syntax: class className()
{
}
2- How to Create a variable in PHP?
Objects include in the terms of data stored in variables is known as properties. When we declare class variables, then they must be declared with the ‘var’ keywords. Variables that are not declared as class variables are not declared with the var keyword. Properties to variables inside the class. Properties can accept values like strings, integers, and Character, like any other variable. Let’s see in Syntax.
Syntax: class className()
{
var $variable1;
var $variable2;
}
3 – Creating a function in Class
Class functions are known as Methods, they can be used to either manipulate and retrieve data. It is very basic concepts in class and it is like a regular function.
Syntax: class className()
{
var $variable1;
var $variable2;
function myFunction()
{ }
}
4 – Instantiating a Class
We saw how to create the class and how to create the variable. Let us see Instantiating a class. The process of creating an object is known as Instantiation. You can create an object by using variables which is store the objects and using new keyword and the name of a class to initiate form.
Syntax: $objectName= new nameofClass();
4- Simple example of Class PHP
We will need to learn how to create a basic class. Let us see the example of simple class.
The <?php is important part in PHP, It indicates starts point of PHP and the ?> indicates exit point of PHP. These entrance and exit points identify the code as PHP, and both are used in every program of PHP. Class Car is created by using ‘class keyword.’ Define an object attribute(variable) the Car’s color. After next line define the class function name is model() and In that print model name with use of echo keyword. At last end of the class.
Object in PHP:
An Object is run time entity which can stores information/data on how to process that data. Objects are “things” that have attributes and behaviors, just like any real-life objects. Classes are defined with the class keyword and Create Object of class by using new keyword. We can create multiple objects in same class.
Syntax :
$A = new car();
$B = new car();
Let us take the real-time example of a Car:
In above picture, we explain easy example of object and class. Class is like Car blueprint. Before car is designed, there is car blueprint. In this pict, Car blueprint is Class and your actual Cars(A,B,C) are Object. Object have different attribute and behaviours.
Attributes of Car:
- Length and Width
- Color
- No. of doors
- Automatic or manual
- Petrol/Diesel
Behavior of car:
- In some case attributes have effects on behaviours of car. For ex. The size of car engines are effect on cars speed.
Program :
<? Php
class car
{
function speed_calc()
{
echo “Display Speed”;
}
}
$cars = new car();
$cars->speed_calc();
?>
In above program,we showing the actual structure of class and object. The <?php indicates starts point of PHP and the ?> indicates end point of program. These are starts and end points of PHP program. In that we declare class using class keyword. After next line declare function speed_calc() and the echo keyword used to print display speed. Using ‘$’ create object of class. The use of ‘->’ symbol is to accessing the properties and methods of given object.
Leave A Comment