PHP OOP
<?php
//1. Class and object
/*
object-oriented concept
an object can be almost any items or concept- a physical object(table) or conceptual software component(file)
an object is a unique and identical collection of stored data and operations that operate on that data
class attributes are properties or member variables
class operations are methods or Functions,actions, support overloading
Three features:
1.encapsulation: known as data hiding
access to data within an object is available only via the object's operation, known as interface of the object
2.polymorphism: different classes have different behaviors for the same operation.
polymorphism is more a characteristic of behaviors than it is of objects, only functions of a class can be polymorphic
3. inheritance: a subclass inherits attributes and operations from its superclass
if a sentence about 2 classes makes sense with "is a " relation, inheritance is probably appropriate
eg: a car is a vehicle, so car can inherit from vehicle
*/
class Vehicle
{
public $name; //do not omit modifier for attribute, can be accessed inside or outside class
public $speed = 10;
protected $price =100 ; //can be accessed inside class and subclass inherited from it
private $key =3; //can be accessed only within class
//A constructor is automatically called when a object is created, it is for initialization of setting sensible starting value
// public function __construct() //public modifier by default for function
//function __construct(){} ////no parameter by default,can be passed 0 or more thann one parameters
//function __construct($this->name) //wrong can not use this as a parameter
function __construct($name) //functions starting with __ change and set the value of any attributes that are inaccessible
{
echo "this is a $name"."<br>"; //$name is a new local variable to be passed in from outside , has nothing to do with $this->name
$this->name = $name; //to get value from outside class to initiate class name attribute
}
//setter will be invoked when something outside class access inaccessible variables, for encapsulation
//including protected, private and not declared variable,
// but public variable will not invoke the setter and getter, because we can access public variable
public function __set($property, $value)
{
if ($value > 100)
{
$current =$this->$property;
//notice: $this->property(without $) will create a new attribute called property, $property
$this->$property= $value;
echo "You are updating $property from $current to $value"."<br>";
}else{
echo "Update $property failed! the $property must be greater than 100"."<br>";
}
}
public function __get($property)
{
return "You are trying to get inaccessible $property 's value: " . $this->$property . " via getter <br>";
}
public function move() //by default
{
$this->start();
echo "$this->name is moving now"."<br>";
}
protected function openDoor() //can be used inside of inherited class, not outside subclass
{
echo "the door is openning now"."<br>";
}
private function start()
{
echo "$this->name is starting now"."<br>";
}
// A destructor is called automatically when all reference to the class have been unset or a a class is destroyed
//notice: it is the class that is destroyed, not the object of that class
function __destruct() //can not take parameters
{
// TODO: Implement __destruct() method.
}
}
//2. Instantiating Classes
$veh = new Vehicle('Bench'); //this is a Bench, 'Bench' will be passed to constructor and it is called automatically
//inside class using this pointer to access attributes and functions
//outside class using the reference to object to access attributes and functions
$veh->move();// Bench is moving now
echo $veh->name; //Bench
$veh->price = 20; //Update price failed! the price must be greater than 100, accessing protected variable, invoke setter
$veh->price = 120; //You are updating price from 100 to 120, accessing protected variable, invoke setter
echo $veh->price ;//You are trying to get inaccessible price 's value: 120 via getter
echo $veh->key; //You are trying to get inaccessible key 's value: 3 via getter
//3.Implementing inheritance
class Car extends Vehicle {
public $owner ='luoxu';
public function drive(){
echo "$this->owner is driving now <br>";
}
function toCallOpenDoor(){
$this->openDoor();
}
}
//as a subclass of Vehicle, Car has all the same functionality and data
//Car has declared an attribute and an operation of its own
//$carObj = new Car(); //constructor also inherit from Vehicle, must pass a parameter into otherwise wrong
$carObj = new Car("myCar");
echo 'calling public attribute :'.$carObj->speed."<br>";//10
echo 'calling protected attribute :'.$carObj->price; //You are trying to get inaccessible price 's value: 100 via getter
$carObj->price=334; //You are updating price from 100 to 334
echo 'after changing protected attribute :'. $carObj->price; //You are trying to get inaccessible price 's value: 334 via getter
echo 'calling its own public attribute :'.$carObj->owner."<br>"; //luoxu
$carObj->move();//myCar is starting now myCar is moving now
$carObj->drive(); //luoxu is driving now
//$carObj->openDoor(); //protected function can not be used outside subclass
$carObj->toCallOpenDoor(); //the door is openning now
echo ($carObj instanceof Car); //true
echo ($carObj instanceof Vehicle); //true
echo "<hr>";
//4. overriding function and variable
//preventing inheritance and overriding with final
class Person{
const HANDS=2;
public $name = "Joseph";
protected $age =24; //default value
public $pocket ="pocket money";
private $treasure ='Gold';
function read(){
echo "$this->name is reading CLASSIC book <br>";
echo "He is looking for $this->treasure and $this->pocket <br>";
}
final public function run(){
echo "Person runs at normal speed !";
}
public function showInfo(){
echo "$this->name has ".self::HANDS."hands";
}
}
class Kid extends Person{
public $age =8; //inherit protected variable $age, and overriding its default value
public function read() //overriding superclass's function read()
{ // :: can be used in static, const, overridden function
parent::read(); // we could call parent class's function by using keyword parent,
echo "He is $this->age years old <br>"; //overriding parent's methods
}//although parent::read() call the superclass method, php uses the attribute values from current class unless it is not overwritten
//luoxu is reading CLASSIC book //luoxu is from current(sub) class
//He is looking for Gold and pocket money //Gold and pocket money is from superclass because they are not provided in the current subclass
////He is 8 years old //8 is from current(sub) class
// public function run() //can not be overridden with final function
}
$k = new Kid();
$k->name = "luoxu"; //access attribute directLy from parent class
$k->read();
//5.Implementing Interfaces
//php doesn't support mulitiple inheritance, which means that each class can inherit from only one parent
//multiple inheritance can be confusing from a maintenance perspective, so various mechanism have been developed
// to exploit the advantages of multiple inheritance without paying maintenance cost.
//interface and trait are such mechanism
interface Eatable{
function eat();
}
interface Sleepable{
function sleep();
}
class Adult extends Person implements Eatable,Sleepable { //inherit from one class an multiple interfaces
public $name ="Adult person" ;
public function eat() //has to implement all function in the interface Eatable
{
// TODO: Implement eat() method.
echo "$this->name is eating ";
}
public function sleep() //has to implement all function in the interface Eatable
{
// TODO: Implement sleep() method.
}
}
$aObj = new Adult();
$aObj->eat(); //Adult person is eating
//6. Using traits
//th key difference between traitsand interfaces is that traits include an implementation, as opposed to merely
// specifying an interface that must be implemented
//Example 1
trait logger{
public function logMsg($message, $level ="DEBUG"){
echo "write $message to a logger in traits <br> ";
}
}
class file{
public function logMsg(){
echo "write $message to a file parent class parent class <br>";}
}
class fileStorage extends file{
use logger;
function store($data,$msg){
//……
$this->logMsg($msg); //calling function in traits
}
public function logMsg(){
echo "write $message to a file Storage current class <br>";}
}
$fs = new fileStorage();
$fs-> store('data','message to be showed!');
//if those functions have same name, priorities: current class function > traits function > parent function
//Example 2
//one nice thing about traits is that you can combine multiple traits which have methods with the same name
trait fileLogger{
public function logMsg($message, $level ="DEBUG"){
echo "write $message to a log file<br> ";
}
}
trait sysLogger{
public function logMsg($message, $level ="ERROR"){
echo "write $message to a syslog <br> ";
}
}
class loggerStorage{
use fileLogger, sysLogger{ //Trait method 'logMsg'in sysLogger collides with 'fileLogger'
//we should specify which one to use
fileLogger::logMsg insteadof sysLogger; //this line explicitly tells php to use logMsg() from fileLogger trait
sysLogger::logMsg as private logSysMsg; //rename logMsg in sysLogger, and also set visibility
//however, we also want to access logMsg() from sysLogger, we rename it using keyword 'as'
}
function store($msg){
//......
$this->logMsg($msg);
$this->logSysMsg($msg);
}
}
$ls = new loggerStorage();
$ls->logMsg('using method in trait like member method');
//$ls->logSysMsg(); private modifier ,inaccessible
$ls->store("message for storage");
//7. Display a large block of static HTML without processing with PHP
//you simply use an end php tag(? >), type your HTML, then re-enter php with an open php tag(<? php)
function displayHTML($size){
echo "codes below will show HTML codes to browser to display a table<br>";
?>
<!DOCTYPE HTML>
<html>
<body>
<table border="1">
<tr><td>this is a <?=$size?>block of static HTML tags </td></tr> //display variable value in html code
<tr><td>this is a <?=$size?> block of static HTML tags </td></tr>
<tr><td>this is a <?=$size?>block of static HTML tags </td></tr>
</table>
</body>
</html>
<?php
echo "html codes above";
}
displayHTML("large ");
//8.Advanced OOP Features
//1.using per-class consonant and static methods
class Math {
const PI =3.14; //this constant can be used without instantiate the class
static function area ($radius){
echo Math::PI; // way to access constant
echo self::PI; // way to access constant
return Math::PI * $radius *$radius;
//$this->PI don't use this because there may be no object instance to refer to
}
}
echo Math::PI; // way to access constant
echo Math::area(29); // way to access static function
//2. instanceof usage : echo ($carObj instanceof Car); //true
//type hint
function checkHint(Math $m, String $s,int $i,bool $b, array $a,callable $c){
//...... type hint for interface https://phpenthusiast.com/object-oriented-php-tutorials/type-hinting-for-interfaces
//type hint does not work for trait
}
// checkHint(new Math(),'string',1,true,[],displayHTML('large')); //type must match correspondingly
//3.late static bindings, late means call whichClass() a bit later for subclass B at runtime
class A{
public $name;
public $age;
public static function whichClass(){
echo __CLASS__;
}
public static function test(){
// self::whichClass(); //B::test() outputs A //because test() is executed in the context of the parent class A
static::whichClass(); //B::test() outputs B //static modifier makes php use class that was actually called at runtime
}
//This method will be invoked by a clone operator and will prepend "Copy " to the name and age properties.
public function __clone(){
// TODO: Implement __clone() method.
$this->name = "COPY".$this->name;
$this->age = "COPY".$this->age;
}
function __call($funName, $arguments)
{
echo "The function you called:" . $funName . "(parameter:" ; // Print the method's name that is not existed.
print_r($arguments); // Print the parameter list of the method that is not existed.
echo ")does not exist!!<br>\n";
}
}
class B extends A{
public static function whichClass(){
echo __CLASS__;
}
}
A::test(); //both are A
B::test();
//4.__clone is invoked by use of the clone keyword. It is used to manipulate object state upon cloning,
// after the object has been actually cloned
$a =new A();
$a->name ="Joseph";
$a->age =23;
$copyA = clone $a; // triggers the __clone magic method, $copyA is a second copy data, independent with $a
echo $copyA->name." ".$copyA->age; //COPYJoseph COPY23
$a->name ="luoxu";
$a->age =25;
echo $copyA->name." ".$copyA->age; //COPYJoseph COPY23
//5. Abstract class, which can not be instantiated, as well as abstract method
//to make sure every subclass contains or overrides some particular function
//this can be done with an interface
abstract class ClsAbstract{
abstract function test();
}
//6.__call()
$a->run("teacher",11);//The function you called:run(parameter:Array ( [0] => teacher [1] => 11 ) )does not exist!!
// If the method which is not existed is called within the object, then the __call() method will be called automatically.
//7.__autoload() check here https://www.tutorialdocs.com/article/16-php-magic-methods.html
//9. Iterator
//Using foreach to iterate through the attributes of an object
class myClass{
public $a= 1;
public $b= 2;
public $c= 3;
}
$x = new myClass();
foreach($x as $attribute ){
echo $attribute; //123
}
//get more info here https://www.startutorial.com/articles/view/modern-php-developer-iterator
class ArrIterator implements Iterator{ // implements Traversable
//Interface for external iterators or objects that can be iterated themselves internally
private $arr;
private $index;
private $length;
function __construct($obj){
$this->arr =$obj->data;
$this->length = count($obj->data);
}
/**
* @inheritDoc
*/
public function current() ////abstract method from iterator
{
// TODO: Implement current() method.
return $this->arr[$this->index];
}
public function next()
{
// TODO: Implement next() method.
$this->index++;
}
public function key()
{
// TODO: Implement key() method.
return $this->index ;
}
public function valid()
{
// TODO: Implement valid() method.
return $this->index < $this->length;
}
public function rewind()
{
// TODO: Implement rewind() method.
$this->index =0;
}
}
class ObjArr implements IteratorAggregate{ //// implements Traversable
/// //Interface to create and get an external Iterator.
public $data = array();
/**
* @inheritDoc
*/
function __construct($arr)
{
$this->data = $arr;
}
public function getIterator() ////abstract method from iterator
{
// TODO: Implement getIterator() method.
return new ArrIterator($this);
}
}
$oa = new ObjArr(array(3,4,5,5,6,77)); ////iterate array in a object-oriented mode
$iterator = $oa->getIterator();
for($iterator->rewind();$iterator->valid();$iterator->next()){
echo $iterator->key().'=>'.$iterator->current();
}
//10. Generator, check out more https://alanstorm.com/php-generators-from-scratch/
//the key difference between a generator and a function that fills a array with all possible values, is that it uses lazy execution
//Only one value is created and held in memory at any time, generator is helpful in large dataset that will not easily fit in memory.
//Generators are a special type of function in PHP that always returns a Generator object. Generator function
// definitions are similar to regular function definitions, with one exception.
// Instead of using a return keyword, they use a yield keyword.
// Generator implements Iterator {
// /* Methods */
// public current ( void ) : mixed
// public getReturn ( void ) : mixed
// public key ( void ) : mixed
// public next ( void ) : void
// public rewind ( void ) : void
// public send ( mixed $value ) : mixed
// public throw ( Throwable $exception ) : mixed
// public valid ( void ) : bool
// public __wakeup ( void ) : void
// }
function myGeneratorFunction()
{
yield;
}
$returnValue = myGeneratorFunction();
echo get_class($returnValue),"\n"; //Generator
//Although our function is defined with the regular old function keyword, PHP’s internals treat it differently
// because the function includes the yield keyword. PHP will always treat a function that includes the yield keyword
// as a generator function, and a generator function will always return a Generator object
$values = [1,2,3,4,5];
// using foreach
foreach($values as $number) {
echo $number, "\n";
}
// using an iterator
$iterator = new ArrayIterator($values); //Construct an ArrayIterator
while($number = $iterator->current()) {
echo $number, "\n";
$iterator->next();
}
///////////////////////////////////
# 1. Define a Generator Function
function generator_range($min, $max)
{
#3b. Start executing once `current`'s been called
for($i=$min;$i<=$max;$i++) {
echo "Starting Loop","\n";
yield $i; #3c. Return execution to the main program
#4b. Return execution to the main program again
#4a. Resume exection when `next is called
echo "Ending Loop","\n";
}
}
#2. Call the generator function
$generator = generator_range(1, 5); //create a generator object
#3a. Call the `current` method on the generator function
echo $generator->current(), "\n"; //call current function
#4a. Resume execution and call `next` on the generator object
$generator->next(); //execute the rest of loop with increment $i
#5 echo out value we yielded when calling `next` above
echo $generator->current(), "\n"; //
// give this a try when you have some free time
foreach(generator_range(1, 5) as $value) {
echo $value, "\n";
}
echo "<hr>";
//11.__toString() //if __toString() is not provided in the class, echo $person; will produces fatal error
class Doctor
{
public $sex;
public $name;
public $age;
public function __construct($name="", $age=25, $sex='Male')
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
public function __toString()
{
return var_export($this,true);
}
function showInfo(){
echo "Name $this->name";
}
}
$person = new Doctor('John'); // Initially assigned.
echo $person; // Doctor::__set_state(array( 'sex' => 'Male', 'name' => 'John', 'age' => 25, ))
//12. Using reflection API
$class = new ReflectionClass('Doctor'); //get basic info of unknown class
echo "<pre>".$class."<pre>";
echo "<hr>";
//13. Namespaces
//By default, all constant, class and function names are placed in a global space —
//like they were before namespaces were supported
//check here more https://www.sitepoint.com/php-53-namespaces-basics/
//14. Exception
try {
// do something, maybe throw some exceptions
} catch (Exception $e) {
// handle exception
} finally {
echo "Always runs!";
}
class myException extends Exception
{
function __toString() //overrides __toString() change the way it display error message
{
return "<strong>Exception ".$this->getCode()
."</strong>: ".$this->getMessage()."<br />"
."in ".$this->getFile()." on line ".$this->getLine()."<br/>";
}
}
try
{
throw new myException("A terrible error has occurred", 42);
}
catch (myException $m)
{
echo $m;
}
catch (Exception $e)
{
echo $e;
}
PHP OOP的更多相关文章
- c#面向对象基础技能——学习笔记(二)基于OOP思想研究对象的【属性】
字段(成员变量): 字段只能从对象中访问实例字段,无法直接从类中访问(换言之,不创建实例就不能访问),可以理解为:字段一般用在内部数据交互使用,当需要为外部提供数据时,(要优先使用自动实现的属性而不是 ...
- 一个简单oop的changeTab
好多地方都会用到这样一个效果“点击tab切换内容页”,根据自己的想法实现了一下,写了个简单的插件.以前写代码都是标准的函数式编程,现在觉得面向对象编程看起来比较爽,并且更容易维护,于是就用oop的思想 ...
- Python OOP(面向对象编程)
一OOP的作用 在Python中,类是面向对象设计(OOP)的主要工具.通过使用类这种工具,OOP可以: 1.分解代码,最小化代码的冗余. 2.通过定制现有的代码,来编写新的程序,而不用在原处进行修改 ...
- OOP,WEB开发实用小技巧
偶然读到一篇博客,记录一下心得.这种设计对于新手来说一般是想不到的,它充分的发挥了OOP语言的特性,让代码专用而清爽.这是不是重构的思想呢? 我们在写业务层的时候,有很多方法是重复功能的,我们就可以使 ...
- 从OOP的角度看Golang
资料来源 https://github.com/luciotato/golang-notes/blob/master/OOP.md?hmsr=toutiao.io&utm_medium=tou ...
- 玩转JavaScript OOP[2]——类的实现
概述 当我们在谈论面向对象编程时,我们在谈论什么?我们首先谈论的是一些概念:对象.类.封装.继承.多态.对象和类是面向对象的基础,封装.继承和多态是面向对象编程的三大特性. JavaScript提供了 ...
- Atitit 面向对象编程(OOP)、面向组件编程(COP)、面向方面编程(AOP)和面向服务编程(SOP)的区别和联系
Atitit 面向对象编程(OOP).面向组件编程(COP).面向方面编程(AOP)和面向服务编程(SOP)的区别和联系 1. 面向组件编程(COP) 所以,组件比起对象来的进步就在于通用的规范的引入 ...
- iOS - 对OOA、OOD、OOP的理解
很多人在求职的时候,会遇到一个这样的问题:“对OOD/OOP有较深的理解”,这个时候有人就会问OOD.OOP是什么呢?那么今天咱们就一块来看一下OOA.OOD.OOP到底是什么! (一)OOA--面向 ...
- OOP感悟
行内讲的最多的就是出来一年内基本靠copy,一年后才基本懂得如何去写代码,而理解领悟oop,需要的时间却不定. 我工作中,我发现很多人拿着面向对相当的语言作者做着面向过程的事情,不需要知其所以然,只要 ...
- 初识JAVA之OOP
有一段时间没发博客了,每次手打还是很累,但感觉很充实.. 最近发现很多初学者到了面向对象编程这个知识点时,不太清楚类是如何转化成为对象的,很是困扰,今天我在这里谈谈我的理解,大家一起来研究学习... ...
随机推荐
- 关于在osgearth 中 出现 arial.ttf : file not handled 的问题
这是由于配置osg时 freetype 插件没有配置到位. 我个人的解决方法 打开CMAKE,点击advance,不勾选OSG_TEXT_USE_FONTCONFIG. 同时将freetype路径设置 ...
- redis基础知识汇总
- Java开发最佳实践(二) ——《Java开发手册》之"异常处理、MySQL 数据库"
二.异常日志 (一) 异常处理 (二) 日志规约 三.单元测试 四.安全规约 五.MySQL数据库 (一) 建表规约 (二) 索引规约 (三) SQL语句 (四) ORM映射 六.工程结构 七.设计规 ...
- bat常用符合和for语句等
一.开头 @echo off(默认是echo on)@echo off执行以后,后面所有的命令均不显示,包括本条命令 二.特殊符号 1. | 命令管道符,echo Y|rd /s c:\abc,通过管 ...
- k8s系列----索引
day1:k8s集群准备搭建和相关介绍 day2:k8spod介绍与创建 day3:k8sService介绍及创建 day4:ingress资源和ingress-controller day5:存储卷 ...
- [Redis-Python]发布订阅通过Redis异步发送邮件
接收订阅 #!/usr/bin/env pyhton # coding:utf-8 # @Time : 2020-02-16 21:36 # @Author : LeoShi # @Site : # ...
- React之props、state和render函数的关系
1.当组件中的state或者props发生改变的的时候,render函数就会被重新执行 2.当父组件的render函数被运行时,它的子组件的render都将被重新运行一次 3.子组件作为父组件里的一个 ...
- [未完成]ECRound 80
Educational Codeforces Round 80 (Rated for Div. 2)A 大意:寻找x使得等式<=n,若满足情况输出YES否则NO 解法:纯数学题,先构造函数f(x ...
- cache和内存屏障
1 cache简介 1.1 cache缓存映射规则 tag查看cache是否匹配,set index |tag |set index |block offset ||20-bit |7-bit |5b ...
- C语言三 语句练习
输入一个整数day代表星期几,根据day的值输出对应的星期几,比如day==1,就输出“星期一”(用两种方式实现) int Day; printf("请输入一个1~7的数字"); ...