0% found this document useful (0 votes)
0 views

chapter 4- Classes and Objects

Chapter Four covers the fundamentals of Object-Oriented Programming (OOP) in PHP, including the definitions and roles of classes and objects, constructors, destructors, access modifiers, constants, interfaces, static methods and properties, and namespaces. It explains how classes serve as templates for objects, the significance of constructors and destructors in object lifecycle management, and the importance of access modifiers in controlling property and method visibility. The chapter also discusses the use of interfaces for polymorphism and the organization of code through namespaces.

Uploaded by

beshahh21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

chapter 4- Classes and Objects

Chapter Four covers the fundamentals of Object-Oriented Programming (OOP) in PHP, including the definitions and roles of classes and objects, constructors, destructors, access modifiers, constants, interfaces, static methods and properties, and namespaces. It explains how classes serve as templates for objects, the significance of constructors and destructors in object lifecycle management, and the importance of access modifiers in controlling property and method visibility. The chapter also discusses the use of interfaces for polymorphism and the organization of code through namespaces.

Uploaded by

beshahh21
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

Chapter Four

Classes and Objects


Outline
⚫ What is OOP?
⚫ Classes and Objects
⚫ Constructors
⚫ Destructor
⚫ Access Modifiers
⚫ Constants
⚫ Interfaces
⚫ Static Methods and Properties
⚫ Namespaces
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
What are Classes and Objects?
⚫ Classes and objects are the two main aspects of
object-oriented programming.
⚫ 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.
Classes
⚫ 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 Car {
// code goes here...
}
?>
<?php In a class, variables are called
Example class Car{ properties and functions are
// Propertiescalled methods!
public $name;
public $color;

// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>
Objects
⚫ 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.
⚫ Objects of a class are created using
the new keyword.
Example <?php
//code goes here
$ferrari = new Car();
$volsewagan = new Car();
$ferrari>set_name(‘Ferrari');
$porsche->set_name(‘Porche');

echo $ferrari->get_name();
echo "<br>";
echo $porsche->get_name();

?>
1.
Inside the class (by 2. Outside the class (by directly
adding a set_name() changing the property value):
method and use $this):
<?php
<?php
class Car { class Car {
public $name;
function set_name($name) { public $name;
$this->name = $name; }
}
} $ferrari = new Car();
$ferrari = new Car(); $ferrari->name = “Ferrari";
$ferrari->set_name(“Ferrari");
echo $ferrari->name; echo $ferrari->name;
?>
?>
Constructor
⚫ A constructor allows you to initialize an
object's properties upon creation of the
object.
⚫ If you create a __construct() function,
PHP will automatically call this function
when you create an object from a class.
⚫ Note that the construct function starts
with two underscores (__)!
Example
<?php

class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
$apple = new Fruit("Apple");
echo $apple->get_name();
?>
Destructor
⚫ A destructor is called when the object is
destructed or the script is stopped or
exited.
⚫ If you create a __destruct() function, PHP
will automatically call this function at the
end of the script.
⚫ Note that the destruct function starts with
two underscores (__)!
Example
<?php

class Fruit {
public $name;
public $color;
function __construct($name) {
$this->name = $name;
}
function __destruct() {
echo "The fruit is
{$this->name}.";
}
}
$apple = new Fruit("Apple");
Questions?
Access Modifiers
⚫ Properties and methods can have access modifiers
which control where they can be accessed.
There are three access modifiers:
•public - the property or method can be accessed from
everywhere. This is default
•protected - the property or method can be accessed
within the class and by classes derived from that class
•private - the property or method can ONLY be accessed
within the class
class Fruit {
public $name;
protected $color;
private $weight;
}
Class Constants

⚫ Class constants can be useful if you need to


define some constant data within a class.
⚫ A class constant is declared inside a class with
the const keyword.
⚫ A constant cannot be changed once it is
declared.
⚫ Class constants are case-sensitive. However, it is
recommended to name the constants in all
uppercase letters.
⚫ We can access a constant from outside the class
by using the class name followed by the scope
using the self keyword followed by the scope
Example resolution operator (::)
followed by the constant name
<?php
<?php class Goodbye {
class Goodbye { const LEAVING_MESSAGE
const LEAVING_MESSA = "Thank you for visiting
ilearn.com!";
GE = "Thank you for public function byebye() {
visiting ilearn.com!"; echo self::LEAVING_MESSAGE;
} }
}

echo Goodbye::LEAVING_ $goodbye = new Goodbye();


MESSAGE; $goodbye->byebye();
?> ?>
Interfaces

⚫ Interfaces allow you to specify what


methods a class should implement.
⚫ Interfaces make it easy to use a variety of
different classes in the same way. When
one or more classes use the same
interface, it is referred to as
"polymorphism".
⚫ Interfaces are declared with
the interface keyword:
Example
<?php
interface InterfaceName {
public function someMethod1();
public function someMethod2($name, $color);
public function someMethod3() : string;
}
?>
Using Interfaces
⚫ Implement an interface, a class must use
the implements keyword.
⚫ A class that implements an interface must implement all of the
interface's methods.
<?php
interface Animal {
public function makeSound();
}
class Cat implements Animal {
public function makeSound() {
echo "Meow";
}
}
$animal = new Cat();
$animal->makeSound();
?>
Static Methods

⚫ Static methods can be called directly - without


creating an instance of the class first.
⚫ Static methods are declared with
the static keyword:
Syntax
<?php

class ClassName {
public static function staticMethod() {
echo "Hello World!";
}
}
?>
⚫ To access a static method use the class name,
double colon (::), and the method name:
ClassName::staticMethod();
<?php
class greeting {
public static function welcome() {
echo "Hello World!";
}
}

// Call static method


greeting::welcome();
?>
The $this Keyword
library 1

⚫ The $this keyword refers to the current object, and


is only available inside methods.
⚫ Look at the following example:

<?php
class Car {
public $name;
}
$ferrari = new Car();
?>
So, where can we change the value of the $name property?
There are two ways:
⚫ A class can have both static and non-static
methods. A static method can be accessed from a
method in the same class using the self keyword
and double colon (::)
⚫ Static methods can also be called from
methods in other classes. To do this, the
static method should be public:
⚫ To call a static method from a child class, use
the parent keyword inside the child class. Here,
the static method can be public or protected.
Static Properties
⚫ Static properties can be called directly -
without creating an instance of a class.
⚫ Static properties are declared with
the static keyword:
Syntax
<?php
class ClassName {
public static $staticProp = "W3Schools";
}
?>
ClassName::$staticProp;
Namespace
⚫ Namespaces are qualifiers that solve two
different problems:
⚫ They allow for better organization by grouping
classes that work together to perform a task
⚫ They allow the same name to be used for more
than one class
Declaring a Namespace

⚫ Namespaces are declared at the beginning


of a file using the namespace keyword:
Syntax
<?php
namespace Html;
?>
Note: A namespace declaration must be the first thing in the
PHP file.
Using Namespaces

⚫ Any code that follows a namespace declaration


is operating inside the namespace, so classes
that belong to the namespace can be
instantiated without any qualifiers.
⚫ To access classes from outside a namespace, the
class needs to have the namespace attached to
<?php
it.
$table = new Html\Table();
$row = new Html\Row();
?>
Namespace Alias

⚫ It can be useful to give a namespace or


class an alias to make it easier to write.
This is done with the use keyword:
Give a namespace an alias: Give a class an alias:
<?php <?php
use Html as H; use Html\Table as T;
$table = new H\Table(); $table = new T();
?> ?>
Take away questions
1. How do you define a class in PHP, and how do you create
an object from it?
2. What is a constructor in PHP, and how is it used in a
class?
3. What are the three access modifiers in PHP, and how do
they affect class properties and methods?
4. What is a static property or method in PHP, and how is it
accessed?
End of Chapter Four

You might also like