News

Object Oriented PHP for Beginners

oops-PHP

Object oriented programming is something that scares many beginners and young programmers. If you are a first year computer science student then make sure you learn this stuff well because object oriented programming is something you are going to deal all through, in fact mostly in your programming life.

This tutorial aims to teach the basics of object oriented programming. It is designed for the students who have some knowledge about procedural programming. And it is tailored specifically for PHP programming language. Probably the most important think I would like to take away from this tutorial is the idea of object oriented as it requires a new way of thinking.  Let’s get started

What is object oriented programming in PHP?

Well object oriented PHP or some may call it as OOP in PHP is very much similar to object oriented programming in JAVA and C++. Looking at a object oriented code it looks far more complicated and complex than a procedural or inline programming code. But the truth is OOP is simple and straight forward and better way to program.

Like any other programming language object oriented PHP follows the rules of creating “classes” and there “objects”.

Right of the bat, there is a huge confusion between objects and classes. So what exactly is the difference then?

What are classes in PHP?

A class is something like a blue print of a house on a paper. It is the layout of the entire house, the size, the dimensions and much more. This blueprint or the class also explains the interconnection and relations between different parts of the house. Similarly classes in PHP are collection of different methods and objects.

What are objects in PHP?

It might be clear from the above definitions of the class, now an object in PHP is something like a house.  An object is any entity that can be manipulated by the commands of a programming language, such as a value, variable, function, or data structure. It also refers to a particular instance of a class.

Creating a class in PHP

To create a class in PHP you have to follow its syntax and it is straightforward, Just use ‘class’ keyword followed by class-name(user defined).

class Website
{
// Variables
//Methods
}
?>

After creating the class, you need to instantiate the class, which is done using ‘new’ keyword. This object is called a Instance of a class.

$obj = new Website;

 Creating methods for a class in PHP

Like any other programming language PHP allows you to define methods in a class. These methods can only be accessed through the instance of this class created or inheriting class, which you will study later in the concept of inheritance.

Here is an example to explain you class definition, instantiation and its usage.

<?php
// class definition
class Website
{
// method definition

public function display($name)
{
return($name);
}
}
$website = “thegeeksclub”;
// object creation for a class

$obj = new Website;
// method call
echo $obj->display($website);
?>

To access the functions of a class, use  ” -> ” operator along with the method or function name.

$obj -> display($name);

Check out the other tutorials on PHP as well :

PHP Tutorial 1: Introduction for Beginners
PHP Tutorial 2: PHP Script Syntax and HTML File Compatibility
PHP Tutorial 3: Variables in PHP
PHP Tutorial 4: Control Statements
PHP Tutorial 5: Functions
PHP Tutorial 6: Form Processing
PHP Tutorial 7: $_POST and $_GET Functions
PHP Tutorial 8: Connecting PHP script to MySQL
PHP Tutorial 9: PHP insert statement
PHP Tutorial 10 : PHP Session
PHP tutorial 11: Uploading a file to the server

Click to comment

Leave a Reply

Your email address will not be published. Required fields are marked *

To Top