Integrative Programming Technology - W8 PHP Oop
Integrative Programming Technology - W8 PHP Oop
At SJPIICD, I Matter!
LEARNING INTENT!
Terms to Ponder
Essential Content
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:
<?php class
Fruit {
// code goes here...
}
?>
IT105-Integrative Programming | 86
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIO N TECHNOLOGY
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
?>
Define 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.
IT105-Integrative Programming | 87
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIO N TECHNOLOGY
In the example below, $apple and $banana are instances of the class
Fruit:
<?php class
Fruit { //
Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
}
IT105-Integrative Programming | 88
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIO N TECHNOLOGY
In the example below, we add two more methods to class Fruit, for
setting and getting the $color property:
<?php class
Fruit { //
Properties
public $name;
public $color;
// Methods
function set_name($name) {
$this->name = $name;
}
function get_name() {
return $this->name;
}
function set_color($color) {
$this->color = $color;
}
function get_color() {
return $this->color;
}
}
IT105-Integrative Programming | 89
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIO N TECHNOLOGY
<?php class
Fruit {
public $name;
}
$apple = new Fruit();
?>
So, where can we change the value of the $name property? There are
two ways:
IT105-Integrative Programming | 90
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIO N TECHNOLOGY
<?php class
Fruit {
public $name;
function set_name($name) {
$this->name = $name;
}
}
$apple = new Fruit();
$apple->set_name("Apple");
?>
<?php class
Fruit {
public $name;
}
$apple = new Fruit();
$apple->name = "Apple";
?>
The child class will inherit all the public and protected properties
and methods from the parent class. In addition, it can have its own
properties and methods.
IT105-Integrative Programming | 91
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIO N TECHNOLOGY
<?php class
Fruit { public
$name; public
$color;
public function __construct($name, $color) {
$this->name = $name;
$this->color = $color;
}
public function intro() {
echo "The fruit is {$this->name} and the color is {$this-
>color}.";
}
}
Example Explained
This means that the Strawberry class can use the public $name
and $color properties as well as the public __construct() and intro()
methods from the Fruit class because of inheritance.
IT105-Integrative Programming | 92
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIO N TECHNOLOGY
<?php
function writeMsg() {
echo "Hello world!";
}
writeMsg(); // call the
function
?>
IT105-Integrative Programming | 93
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIO N TECHNOLOGY
<?php
function familyName($fname, $year) {
echo "$fname Refsnes. Born in $year <br>";
}
familyName("Hege", "1975"); familyName("Stale",
"1978");
familyName("Kai Jim", "1983");
?>
IT105-Integrative Programming | 94
ST. JOHN PAUL II COLLEGE OF DAVAO
COLLEGE OF INFORMATION AND
COMMUNICATIO N TECHNOLOGY
SELF-SUPPORT: You can click the URL Search Indicator below to help you further
understand the lessons.
Search Indicator
https://www.w3schools.com/php/php_functions.asp
https://www.tutorialspoint.com/php/index.htm
IT105-Integrative Programming | 95