1 of 12

PHP Programming

OOP

aka PHPoop

2 of 12

OOP Concept Review

  • Object Oriented Programming
  • Creating new variable types
  • Class vs. Object
  • Encapsulation
    • variables
    • getters/setters
    • constructors
    • toString

3 of 12

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;

}

?>

4 of 12

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);

5 of 12

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;

}

?>

6 of 12

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;

7 of 12

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();

8 of 12

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)
  • __set($property, $value)
  • __isset($property)
  • __unset($property)

9 of 12

__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;

10 of 12

__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;

11 of 12

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/>";

}

12 of 12

Practice

  1. Design and implement a Dice class. The Dice class models a die, with a number of sides and current value. Be sure to include constructors, getters, setters and any necessary helper methods. One such helper method might be the roll method that generates a random number between 1 and the number of sides. -I know that the singular of Dice is Die, but Die is a keyword in PHP, so I had to go with Dice :)-
  2. Create a Risk game. In this game, the attacking player rolls three dice and the defending player rolls two. Show the rolls for each player. Then, find the highest attacking value and highest defending value. If the defender is >= the attacker, the attacker loses an army. Otherwise the defender loses an army. The same happens with the attackers second highest and the defenders second die. Note how many armies each player loses.

More Practice