What is object-oriented programming in PHP and how to use it
If you are pursuing web development courses and just started learning PHP then you must know about this language in detail. Let’s read about it. PHP a hypertext preprocessor also known as personal home page in earlier days, it is a server side scripting language, we use php with any front end languages, such as html, or even javascript. From the version update of php5 we can now also use the object oriented programming style in the php. Object oriented programming is faster and easier to maintain and execute in compare to the procedural programming. And object oriented programming is all about creating objects of class which can use both the data and the functions. Object oriented programming has some advantages as:
• It is faster and easier to execute
• Has a clear structure
• It follows the concept of do not repeat yourself.
There are numerous ways to master this language and one such way is to go for a PHP training institute.
Object oriented programming is based on the class and their objects, we can also connect it with the real world scenario to understand it better.
Let’s say we are talking about the cars, so in oops the car is the class and the types of cars are the objects of the car class.
Class: Car
Object: Audi, BMW, Volvo
Let’s see how we can use object oriented programming in the php,
<?php
class phpClass {
var $var1;
var $var2 = "constant string";
function myfunc ($arg1, $arg2) {
[..]
}
[..]
}
?>
It is a special form of class, we can define class by defining any name we want to use, in the above example we have used the class name as phpClass, the curly braces is used to wrap up the variables and the functions defined between the class boundaries of the php class.
Let’s see another example of the class in php object oriented programming concept:
<?php
class Books {
/* Member variables */
var $price;
var $title;
/* Member functions */
function setPrice($par){
$this->price = $par;
}
function getPrice(){
echo $this->price ."<br/>";
}
function setTitle($par){
$this->title = $par;
}
function getTitle(){
echo $this->title ." <br/>";
}
}
?>
Here In the above given example, the “this” keyword is used to refer the current object of the class, it works same as the javascript concept of this keyword, if you are already familiar with javascript then it will be more easy for you to understand the concept of this keyword the object oriented php as well. To create new object of a class, we have to use the new keyword operator, it is used to initiate the new objects in the class. For example:
$physics = new Books;
After creating the objects of the class, we can also call the members functions of the class, here is the syntax:
$physics->setTitle( "Physics for High School" );
Constructor:
Constructor is a special type of function which do automatically called whenever a class is being called, we can define a constructor in php by using this syntax:
__construct()
And we can pass as many arguments as we wants in the constructor.
function __construct( $par1, $par2 ) {
$this->title = $par1;
$this->price = $par2;
}
That’s how we can use object oriented concept of programming in the php language. From making a staic site dynamic to developing powerful site application projects, PHP is indeed an amazing language to learn in any web development course.
Top comments (0)