Вы находитесь на странице: 1из 28

Basics of OOP

What are objects? Code grouped by common theme


(grouping code by a common theme)

(student: 1st

name, last name, ID, address, age, gender, height)

Abstractions of code structures(db obj, crud obj)


Functions Attributes

Basics of OOP
Typical code
$student1_first = John; $student1_last = Doe; $student2_first = Mary; $student2_last = Smith; full_name($first, $last){ return $first. .$last; } $student1_courses = array(English,Science,Math);

Why are objects useful?


Code organization and maintainability
you go to the class)
(if you want to change code,

Add clarity, reduce complexity (ex. No matter complex the code is, the
function, we can think of it as a Student, wherein we can ask data to it)

Simple rules allow complex interactions

(ex. Put the students into the classroom, but before doing that, you can as the object to see if its full, or there is no conflict in schedule and then it can update the class.)

Emphasizes data over procedure Code modularity (practical for team of developers) Code reusability (ex. code for shopping cart can be used for other clients) Well-suited for databases (student, table in a db can be a object)

Defining classes

Defining classes
<?php

class Person{
} $classes = get_declared_classes(); foreach($classes as $class){ echo $class.<br />; } If(class_exists(Person)){ echo That class has been defined.<br/>; }else{ echo Class not defined!<br/>; } ?>

Defining class methods

Defining class methods


class Person { function say_hello() { echo "Hello from inside a class.<br />"; } } $methods = get_class_methods('Person'); foreach($methods as $method) { echo $method . "<br />"; } if(method_exists('Person', 'say_hello')) { echo "Method does exist.<br />"; } else { echo "Method does not exist.<br />"; } ?>

Instantiating a class

Instantiating a class
class Person { function say_hello() { echo "Hello from inside a class.<br />"; } } $person = new Person(); $person2 = new Person(); echo get_class($person) . "<br />"; if(is_a($person, 'Person')) { echo "Yup, it's a Person.<br />"; } else { echo "Not a Person.<br />"; } //activate the functionality of the class $person->say_hello(); ?>

Referencing an instance

Referencing an instance
$person = new Person(); $customer = $person;

Referencing an instance
<?php class Person { function say_hello() { echo "Hello from inside the class ". get_class($this) .".<br />"; //$this applies to (ref) instance } function hello() { $this->say_hello(); //$this applies to (ref) instance } } $person = new Person(); $person->say_hello(); $person->hello(); ?>

Defining class properties

Defining class properties/attributes


class Person { var $first_name; var $last_name; var $arm_count = 2; var $leg_count = 2; function say_hello() { echo "Hello from inside the class ". get_class($this) .".<br />"; } function full_name() { return $this->first_name . " " . $this->last_name; } } $person = new Person(); echo $person->arm_count ."<br />"; $person->arm_count = 3; //no dollar sign if its a variable $person->first_name = 'Lucy'; $person->last_name = 'Ricardo';

$new_person = new Person(); $new_person->first_name = 'Ethel'; $new_person->last_name = 'Mertz'; echo $person->full_name() ."<br />"; echo $new_person->full_name() ."<br />";
$vars = get_class_vars('Person'); foreach($vars as $var => $value) { echo "{$var}: {$value}<br />"; } echo property_exists('Person', 'first_name') ? 'true' : 'false'; ?>

INHERITANCE

INHERITANCE
class Car { var $wheels = 4; var $doors = 4; function wheelsdoors() { return $this->wheels + $this->doors; } } class CompactCar extends Car { var $doors = 2; function wheelsdoors() { return $this->wheels + $this->doors + 100; } } $car1 = new Car(); $car2 = new CompactCar(); echo $car1->wheels ."<br />"; echo $car1->doors ."<br />"; echo $car1->wheelsdoors() ."<br />"; echo "<br />"; echo $car2->wheels ."<br />"; echo $car2->doors ."<br />"; echo $car2->wheelsdoors() ."<br />"; echo "<br />";

echo "Car parent: ".get_parent_class('Car') ."<br />"; echo "CompactCar parent: ".get_parent_class('CompactCar') ."<br />"; echo "<br />"; echo is_subclass_of('Car', 'Car') ? 'true' : 'false'; echo "<br />"; echo is_subclass_of('CompactCar', 'Car') ? 'true' : 'false'; echo "<br />"; echo is_subclass_of('Car', 'CompactCar') ? 'true' : 'false'; echo "<br />";

Access Modifiers (restrictions in accessing


attributes/ properties and methods)

Access Modifiers (controlling access from inside the class)


public private protected Everywhere (inside & outside) This class only This class and subclass

Access Modifiers
class Example { //change var to modifiers public $a=1; protected $b=2; private $c=3; function show_abc() { echo $this->a; echo $this->b; echo $this->c; } public function hello_everyone() { return "Hello everyone.<br />"; } protected function hello_family() { return "Hello family.<br />"; } private function hello_me() { return "Hello me.<br />"; } //public by default function hello() { $output = $this->hello_everyone(); $output .= $this->hello_family(); $output .= $this->hello_me(); return $output; } } $example = new Example(); echo "public a: {$example->a}<br />"; echo "protected b: {$example->b}<br />"; echo "private c: {$example->c}<br />"; $example->show_abc(); echo "<br />"; echo "hello_everyone: {$example->hello_everyone()}<br />"; echo "hello_family: {$example->hello_family()}<br />"; echo "hello_me: {$example->hello_me()}<br />"; echo $example->hello();

Setters and Getters

Setters and Getters


// Setters and Getters class SetterGetterExample { private $a=1; public function get_a() { //bank, out return $this->a; } public function set_a($value) { //value as argument, bank in $this->a = $value; } } $example = new SetterGetterExample(); // restricted: echo $example->a ."<br />"; echo $example->get_a() ."<br />"; $example->set_a(15); echo $example->get_a() ."<br />";

Why go on to trouble making it private while we can set it to public?

Bank teller: they check in make sure who we say we are, they make sure that our account has the right amount of money before making a withdraw.
Web application: make sure the user is logged in or not, admin or staff.

Access Modifier: Static Modifier

Static Modifier (static, stays with the class)


// with static methods you can't use $this! class Student { // $student = new Student(); // echo $student->total_students; echo Student::$total_students ."<br />"; //Student or self (class) echo Student::welcome_students() ."<br />"; echo Student::welcome_students("Greetings") ."<br />";

static $total_students=0; static public function add_student() { Student::$total_students++; } Student::$total_students = 1; static function welcome_students($var="Hello") { echo Student::$total_students ."<br />"; echo "{$var} students."; } // static variables are shared throughout the inheritance tree. } class One { static $foo; } class Two extends One { } Scope Resolution Operator class Three extends One { } Paamayim Nekudotayim paam = One One::$foo = 1; ayim = Doubled Two::$foo = 2; nekudot = Dot Three::$foo = 3; ayim = Double echo One::$foo; // 3 Double colon (in Hebrew) echo Two::$foo; // 3 echo Three::$foo; // 3

End of Lecture

Вам также может понравиться