PHP Programming
OOP
aka PHPoop
OOP Concept Review
Getting Started
Just like in Java, we declare the class using the keyword: class.
Additionally, we initiate the process of encapsulation by using the keyword: private.
<?php
class Date{
private $day;
private $month;
private $year;
}
?>
Constructors
The purpose of a constructor is to set ALL of the instance variables. PHP constructors utilize the $this keyword to help assign object values.
** The two underscores (__) denote a pre-defined function name/purpose in PHP. In this case, it is the __construct function.
<?php
…
function __construct($day, $month, $year){
$this->day=$day;
$this->month=$month;
$this->year=$year;
}
…
?>
------------------------------------
$today=new Date(12,5,2000);
Getters/Setters
Getters and setters are written the same. Getters are designed to return instance variable values to the caller. Setters take parameters to change the value of the instance variable.
<?php
…
function getYear(){
return $this->year;
}
function setYear($year){
$this->year=$year;
}
…
?>
toString
The toString method is designed to show the state of the object. In other words, the current value of all instance variables. If we use the built in toString method (__toString), then we need not overtly call the method in an echo statement.
<?php
…
function __toString(){
return $this->month.”/”. � $this->day.”/”.
$this->year;
}
…
?>
-------------------------------
echo $today;
Other Methods
As with any class, you can write whatever methods you choose and/or need. For example, you might choose to have a euroFormat method where the date is day.month.year.
<?php
…
function euroFormat(){
return $this->day.”.”. � $this->month.”.”.
$this->year;
}
…
?>
-------------------------------
echo $today->euroFormat();
Magic Methods
PHP OOP offers something called magic methods. Magic methods are predefined methods in PHP. These methods will can be called automatically and perform certain tasks. Two examples are the __construct and __toString methods that we have already written. Others include:
__get($property)
The __get magic method allows programmers to write all getters in one function AND allows class clients to access variables, seemingly, through direct access with the __get method being called automatically behind the scenes. Here is an example of the __get method.
<?php
…
function __get($propName){
if(property_exists($this, $propName)){
return $this->$propName;
}
}
…
?>
-------------------------------
echo $today->year;
__set($property, $value)
The __set magic method allows programmers to write all setters in one function AND allows class clients to alter variables, seemingly, through direct access with the __set method being called automatically behind the scenes. Here is an example of the __set method.
<?php
…
function __set($propName, $val){
if(property_exists($this, $propName)){
$this->$propName=$val;
}
}
…
?>
-------------------------------
$today->year=2020;
OOP and Databases
If you are reading a lot of information from a database, instead of constantly going back and forth from the tables, it is sometimes easier to read all of the information initially and store it in an array of objects.
$sql="select * from users";
$res=$absConn->query($sql);
while($row=$res->fetch_assoc()){
array_push($users, new User($row['name'],$row['email'], $row['password'], $row['level']));
}
foreach($users as $user){
echo $user;
echo "<br/>";
}
Practice
More Practice