Declaring a class, creating an object
Declaring a class, creating an object
CO3 Understand
Students will learn database connectivity, plugin and security
issues.
2
Web Development
Using PHP
Course Outcome • OOP
CO Title Level
Number
• Session and Cookies
Understand
• File handling
CO1
Students will learn basics of PHP • Database
CO2 Understand &
Students will understand the underlying concept of themes in Analyze
wordpress.
CO3 Understand
Students will learn database connectivity, plugin and security
issues.
3
Topic to be Covered
• Declaring a class
• Creating an object
4
Introduction
• An object is a data type that can store data and functions together in
a single unit. Objects are created using the new keyword followed by
the class name. Once an object is created, you can access its
properties and methods using the arrow notation (->).
• A class can have properties, which are variables that store data, and
methods, which are functions that perform actions on the object.
Classes can also have constructors, which are special methods that
are called when an object is created, and destructors, which are
special methods that are called when the object is destroyed.
5
PHP: Creating classes and Instantiation
• 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 Class_name {
}
?>
6
Cntd..
Below we declare a class named Fruit consisting of two properties ($name
and $color) and two methods set_name() and get_name() for setting and
getting the $name property:
<?php
class Fruit {
// Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
7
?>
Define Objects
• The object is declared to use the properties of a class.
• The object variable is declared by using the new keyword followed by the
class name.
• Multiple object variables can be declared for a class.
• The object variables are work as a reference variable. So, if the property
value of any class is modified by one object then the property value of
another object of the same class will be changed at a time.
Syntax:
8
Example
• <?php
//Declare class
class Product
{
//Declare properties
public $name = "Cake";
public $price = 20;
}
//Declare object
$obj_pro = new Product;
//Print all object properties
print_r($obj_pro);
//Print each property separately
echo "<br />Product Name: ".$obj_pro->name."<br />";
echo "Product Price: ".$obj_pro->price."<br />";
?>
9
Best Practices
• Use classes to create objects and define the properties and methods
of the object.
• Use constructors to set the initial values of an object's properties, and
to perform any necessary setup when an object is created.
• Keep objects small and focused, with a single responsibility. This
makes the code easier to understand and maintain.
• Use clear and descriptive property and method names to make it easy
to understand what the object does and how it is used.
10
CONTINUE……
11
Book References
• Php: The Complete Reference, Steven Holzner, Tata McGraw-
Hill Education, 2007
• Modern PHP: New Features and Good Practices, Josh Lockhart,
"O'Reilly Media, Inc.“, 2015.
• PHP for the Web: Visual QuickStart Guide, Larry Ullman
Pearson Education, 2006.
• https://www.tutorialspoint.com/php/index.htm
• https://www.codecademy.com/learn/learn-php
12
THANK YOU