PHP OOPs Concepts

Basics Of OOPs in PHP

Object oriented programming is a technique to design your application. In object oriented programming, everything will be around the objects and class. By using OOP in php you can create modular web application. By using OOP in php we can perform any activity in the object model structure. There are many benefit of using oop over the parallel or procedural programming. Further in this part we will cover some basic of object and class and its implementation in php.

PHP Class and Objects

In object oriented programming a class can be an abstract data type, blue print or template. You could consider the name of a class as noun like name of a person, place or thing.  For example Fruit is a class, where apple, orange are the object of this class.
Object is the instantiate of a class. A class consists of a member variable and methods. In PHP we need to declare the access specifiers (public, private, or protected) separately.
Objects has two things properties and behaviors. For example car has properties color,brand name, model and behaviors it can forwar or backward.
Exaple for class and Object(How to add two numbers)

class addnumbers
{
var $num1;
var $num2;
function addtwono()
{
return ($this->num1*$this->num2);
}
}

In the above code we can create a class by using class keyword. Here class addnumbers is a class. All the variable start with var keyword is the propert of class. In other word these are variables of the class. we can design own class and function.
Now we can create the object of the class by using new keyowrd.
Example of creation of object of the class addnumbers

$add = new addnumbers();

$add variable is the object of addnumber class. Now we can set the properties of variables and perform addition of two numbers.

$add = new addnumbers();
$add->num1=5;
$add->num2=6;
echo $add->addtwono();

Output : 11
View Demo

Next Tutorial  How to create classes and objects?

Recommended Tutorials for you


Related posts

The __construct() magic method

Anuj Kumar

Magic Methods

Anuj Kumar

Multilevel and Multiple inheritance in PHP

Anuj Kumar

PHP CRUD Operations using PHP OOP and MYSQL

Anuj Kumar

What are exceptions and how to handle them?

Anuj Kumar

OOPs Concept in Php – Offering A Smarter Future for Users

Anuj Kumar

Interfaces – the next level of abstraction

Anuj Kumar

Static methods and properties

Anuj Kumar

Access modifiers: public vs. private

Anuj Kumar

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.