Object-Oriented PHP for Beginners
Drupalers
WORKSHOP
James Candan /jɑnˈdən/
6 years PHP
4 years Drupal
Object-Oriented PHP for Beginners | Tuts+
DEC 23, 2011 / JASON LENGSTORF
http://code.tutsplus.com/tutorials/object-oriented-php-for-beginners--net-12762
@fpvraceways
Agenda
- High level Review OOP concepts
- Setup a remote Drupal instance
- Quickly create a Drupal Migration module
- Write hands-on code to grasp OOP concepts with a practical example
<?php
// Variables
$fruit = "apple";
- Variables
- Functions
- Arrays/Loops
- Logic
<?php
// Variables
$fruit = "apple";
// Logic operators
if ( $fruit == "apple" ) {
// can include html in string output
echo "sweeeeeet!<br />";
}
// Arrays
$colors = array("blue", "purple", 'red');
// Loops
foreach ($colors as $color) {
echo $color . "<br />";
}
<?php
// Variables
$fruit = "apple";
// Logic operators
if ( $fruit == "apple" ) {
// can include html in string output
echo "sweeeeeet!<br />";
}
<?php
// Variables
$fruit = "apple";
// Logic operators
if ( $fruit == "apple" ) {
// can include html in string output
echo "sweeeeeet!<br />";
}
// Arrays
$colors = array("blue", "purple", 'red');
// Loops
foreach ($colors as $color) {
echo $color . "<br />";
}
// Functions
function color_exists($needle, $haystack) {
return in_array($needle, $haystack);
}
// assign returned output to variable
$is_brown_good = color_exists("brown", $colors);
Prerequisites
Recognizing the Differences Between Objects and Classes
- Classes are like blue prints
- Objects are the actual Houses
Properties
Height
Methods
Come
Property values
Color: Gray
Eye Color: Brown
Height: 18"
Length: 36"
Weight: 30 lbs
Methods
Sit
Lay Down
Shake
Come
Create Instance
Object-Oriented Thinking
JULY 16, 2014 / RODRIGO ARAÚJO
http://www.universocomputacao.com/object-oriented-thinking/
Class
Object
Length
Weight
Eye Color
Color
Shake
Lay Down
Sit
Maximus
class RegularPolygon
{
protected $num_sides;
private $radius = 1;
public function __construct($num_sides) {
$this->num_sides = $num_sides;
}
public function set_radius($radius) {
$this->radius = $radius;
}
public function get_angle() {
$sides = $this->num_sides;
return ($sides - 2) * 180 / $sides;
}
public function get_side_length() {
$radius = $this->radius;
$sides = $this->num_sides;
return 2*$radius*sin($M_PI/$sides);
}
public function get_apothem() {
$radius = $this->radius;
$sides = $this->num_sides;
return $radius*cos($M_PI/$sides);
}
}
$hexagon = new RegularPolygon(6);
echo $hexagon->get_angle(); // --> 120
class Rectangle extends RegularPolygon
{
protected $length;
protected $width;
public function __construct($width, $length){
$this->num_sides = 4;
$this->length = length;
$this->width = width;
}
}
class Square extends Rectangle
{
public function __construct($width) {
$this->length = width;
$this->width = width;
}
}
$my_square = new Square();
echo $my_square->get_angle(); // --> 90
$my_hexagon = new RegularPolygon(6);
echo $my_square->get_angle(); // --> 120
Public, Protected, & Private
<?php
class Person
{
private name;
private mobility;
public function get_name() {
return $this->name;
}
public function set_name($name) {
$this->name = $name;
}
public function get_name() {
return $this->name;
}
protected function set_mobility($mobility) {
$this->mobility = $mobility;
}
}
<?php
class Toddler extends Person{
public function __construct($width, $length){
$this->mobility = 'crawling'; // <-- NOT OK.
$this->set_mobility('crawling');
}
}
$my_person = new Toddler();
// can access public elements
$my_person->set_name("Charlie");
// but accessing protected elements would be fatal
echo $my_person->mobility; // --> fatal error
?>
Summary
- Classes are blueprints
- Objects are actual houses
- Classes can have Parent - Child Inheritance via "extends"
- Visibility
- Public - can be seen from anywhere
- Protected - can be seen from Class & Subclass
- Private - can ONLY be seen from within Class
- Static - Doesn't require instantiating (Utilities)
Questions?
OOP PHP Workshop
By James Candan
OOP PHP Workshop
- 1,703