0% found this document useful (0 votes)
91 views5 pages

PHP - What Is OOP?

OOP stands for object-oriented programming which involves creating objects that contain both data and functions, as opposed to just writing procedures in procedural programming. Some advantages of OOP include being faster and easier to execute, providing a clear structure for programs, helping to keep code DRY, and making code easier to maintain and modify. A class defines a template for objects, while an object is an instance of a class that inherits properties and behaviors from the class. The $this keyword refers to the current object and is used inside methods to access object properties and methods.

Uploaded by

Sibtul Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
91 views5 pages

PHP - What Is OOP?

OOP stands for object-oriented programming which involves creating objects that contain both data and functions, as opposed to just writing procedures in procedural programming. Some advantages of OOP include being faster and easier to execute, providing a clear structure for programs, helping to keep code DRY, and making code easier to maintain and modify. A class defines a template for objects, while an object is an instance of a class that inherits properties and behaviors from the class. The $this keyword refers to the current object and is used inside methods to access object properties and methods.

Uploaded by

Sibtul Hassan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Lecture 21 Web Systems and Technologies

PHP - What is OOP?


OOP stands for Object-Oriented Programming.

Procedural programming is about writing procedures or functions that perform operations on


the data, while object-oriented programming is about creating objects that contain both data
and functions.

Object-oriented programming has several advantages over procedural programming:


 OOP is faster and easier to execute
 OOP provides a clear structure for the programs
 OOP helps to keep the PHP code DRY "Don't Repeat Yourself", and makes the code
easier to maintain, modify and debug
 OOP makes it possible to create full reusable applications with less code and shorter
development time

NOTE: The "Don't Repeat Yourself" (DRY) principle is about reducing the repetition of
code. You should extract out the codes that are common for the application, and place them at
a single place and reuse them instead of repeating it.

PHP OOP - Classes and Objects


A class is a template for objects, and an object is an instance of a class.

When the individual objects are created, they inherit all the properties and behaviors from the
class, but each object will have different values for the properties.

Define a Class
A class is defined by using the class keyword, followed by the name of the class and a pair of
curly braces ({}). All its properties and methods go inside the braces:

Syntax
<?php
class Fruit {
// code goes here...
}
?>
Lecture 21 Web Systems and Technologies

The general form for defining a new class in PHP is as follows −


<?php
class phpClass {
var $var1;
var $var2 = "constant string";

function myfunc ($arg1, $arg2) {


[..]
}
[..]
}
?>
Here is the description of each line −

 The special form class, followed by the name of the class that you want to define.

 A set of braces enclosing any number of variable declarations and function


definitions.

 Variable declarations start with the special form var, which is followed by a
conventional $ variable name; they may also have an initial assignment to a
constant value.

 Function definitions look much like standalone PHP functions but are local to the
class and will be used to set and access object data.

Note: In a class, variables are called properties and functions are called methods!
Here is an example which defines a class of Fruit type −
<?php
class Fruit {
// Properties
public $name;
public $color;

// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>
Lecture 21 Web Systems and Technologies

Creating Objects in PHP


Classes are nothing without objects! We can create multiple objects from a class. Each object
has all the properties and methods defined in the class, but they will have different property
values.

Once you defined your class, then you can create as many objects as you like of that class
type. Following is an example of how to create object using new operator.

<?php
class Fruit {
// Properties
public $name;
public $color;

// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}

$apple = new Fruit();


$banana = new Fruit();
$apple->set_name('Apple');
$banana->set_name('Banana');

echo $apple->get_name();
echo "<br>";
echo $banana->get_name();
?>

In the example below, we add two more methods to class Fruit, for setting
and getting the $color property:

Example
<?php
class Fruit {
// Properties
public $name;
public $color;

// Methods
function set_name($name) {
$this->name = $name;
}
Lecture 21 Web Systems and Technologies

function get_name() {
return $this->name;
}
function set_color($color) {
$this->color = $color;
}
function get_color() {
return $this->color;
}
}

$apple = new Fruit();


$apple->set_name('Apple');
$apple->set_color('Red');
echo "Name: " . $apple->get_name();
echo "<br>";
echo "Color: " . $apple->get_color();
?>

PHP - The $this Keyword


The $this keyword refers to the current object, and is only available inside
methods.

Look at the following example:

<?php
class Fruit {
public $name;
}
$apple = new Fruit();
?>

So, where can we change the value of the $name property? There are
two ways:

1. Inside the class (by adding a set_name() method and use $this):

Example
<?php
class Fruit {
public $name;
function set_name($name) {
$this->name = $name;
}
}
$apple = new Fruit();
$apple->set_name("Apple");
?>
Lecture 21 Web Systems and Technologies

2. Outside the class (by directly changing the property value):

<?php
class Fruit {
public $name;
}
$apple = new Fruit();
$apple->name = "Apple";
?>

You might also like