chapter 4- Classes and Objects
chapter 4- Classes and Objects
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 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!";
}
}
<?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