Академический Документы
Профессиональный Документы
Культура Документы
(student: 1st
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);
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)
(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/>; } ?>
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(); ?>
$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
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();
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.
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