Escolar Documentos
Profissional Documentos
Cultura Documentos
1. variables
2. conditionals
3. functions
OBJECT and CLASSES
What is a ‘CLASS’?
A template (for an object) is called a 'class'.
Classes are the blueprints for objects in PHP.
Actually, classes are used in every OO programming
language ever invented! They are one of the
fundamental constructs in object-oriented
programming.
OBJECT and CLASSES
How objects are created:
b. Functions it contains
class Person {
?>
Workshop 1: OOP PHP
Add data to your class:
<?php
class Person {
var $name;
}
?>
function set_name($new_name){
$this->name = $new_name;
}
function get_name(){
return $this->name;
}
}
?>
Workshop 1: OOP PHP
The '$this' variable
$this->name = $new_name;
function get_name() {
return $this->name;
}
Workshop 1: OOP PHP
Include your class in your main PHP page
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-
8859-1" />
<title>OOP in PHP</title>
</head>
<body>
</body>
</html>
Workshop 1: OOP PHP
Instantiate/create your object:
<body>
$clifton = new Person(); Instantiation
</body>
</html>
Workshop 1: OOP PHP
The 'new' keyword
To create an object out of a class, you need to use the 'new' keyword.
<?php include("class_lib.php"); ?>
</head>
<body>
$clifton = new Person();
$rolysent = new Person;
</body>
</html>
For example: with the property $name (in object $stefan,) you could get
its' value like so:
$clifton->name.
• Functions = methods
• Variables = properties
Workshop 1: OOP PHP
Restricting access to properties using 'access modifiers'
One of the fundamental principles in OOP is 'encapsulation'. The idea
is that you create cleaner better code, if you restrict access to the
data structures (properties) in your objects.
1. public
2. private
3. protected
$this->link = "<br>".Person::get_name()."<br>";
$this->link .= Person::get_sss_id()."<br>";
$this->link .= Person::get_account_no();
return $this->link;}
}
OOP