From 445eeb1343855dd9a2e8ff0e9d12ea462cf8f0f4 Mon Sep 17 00:00:00 2001 From: wiwkok-pc <867034892@qq.com> Date: Thu, 17 Mar 2022 23:20:08 +0800 Subject: [PATCH] =?UTF-8?q?=E7=B1=BB=E4=B8=8E=E5=AF=B9=E8=B1=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...iable_variables.php => 04可变变量.php} | 0 03函数/02引用.php | 9 ++ 03函数/04匿名函数.php | 60 +++++++++++-- 03函数/08箭头函数.php | 33 +++++++ 07面向对象/05重载/__set.php | 3 +- ...调用未定义的变量或方式时触发 | 0 ...6抽象类.php => 06抽象类abstract.php} | 5 ++ .../{07接口.php => 07接口interface.php} | 1 + 07面向对象/09克隆clone.php | 3 + 07面向对象/11自动加载类/autoload.php | 23 +++++ .../11自动加载类}/classA.php | 0 07面向对象/11重载.php | 3 - 07面向对象/13静态属性方法static.php | 31 +++++++ 07面向对象/14代码复用trait.php | 20 +++++ 07面向对象/17遍历对象.php | 27 ++++++ 07面向对象/18静态绑定.php | 40 +++++++++ 07面向对象/19对象的引用.php | 26 ++++++ .../classA.php | 13 +++ .../page1.php | 8 ++ .../page2.php | 8 ++ .../store | 1 + 07面向对象/21协变和逆变/01协变.php | 88 +++++++++++++++++++ 07面向对象/21协变和逆变/02逆变.php | 32 +++++++ 11自动加载类/autoload.php | 20 ----- 11自动加载类/classB.php | 6 -- 16其他/01-php执行shell.php | 9 ++ 26 files changed, 432 insertions(+), 37 deletions(-) rename 01基本语法/{variable_variables.php => 04可变变量.php} (100%) create mode 100644 03函数/02引用.php create mode 100644 03函数/08箭头函数.php create mode 100644 07面向对象/05重载/当调用未定义的变量或方式时触发 rename 07面向对象/{06抽象类.php => 06抽象类abstract.php} (78%) rename 07面向对象/{07接口.php => 07接口interface.php} (93%) create mode 100644 07面向对象/11自动加载类/autoload.php rename {11自动加载类 => 07面向对象/11自动加载类}/classA.php (100%) delete mode 100644 07面向对象/11重载.php create mode 100644 07面向对象/13静态属性方法static.php create mode 100644 07面向对象/14代码复用trait.php create mode 100644 07面向对象/17遍历对象.php create mode 100644 07面向对象/18静态绑定.php create mode 100644 07面向对象/19对象的引用.php create mode 100644 07面向对象/20序列化对象-在会话中存放对象/classA.php create mode 100644 07面向对象/20序列化对象-在会话中存放对象/page1.php create mode 100644 07面向对象/20序列化对象-在会话中存放对象/page2.php create mode 100644 07面向对象/20序列化对象-在会话中存放对象/store create mode 100644 07面向对象/21协变和逆变/01协变.php create mode 100644 07面向对象/21协变和逆变/02逆变.php delete mode 100644 11自动加载类/autoload.php delete mode 100644 11自动加载类/classB.php create mode 100644 16其他/01-php执行shell.php diff --git a/01基本语法/variable_variables.php b/01基本语法/04可变变量.php similarity index 100% rename from 01基本语法/variable_variables.php rename to 01基本语法/04可变变量.php diff --git a/03函数/02引用.php b/03函数/02引用.php new file mode 100644 index 0000000..7996e1b --- /dev/null +++ b/03函数/02引用.php @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/03函数/04匿名函数.php b/03函数/04匿名函数.php index 1c0095e..97e1075 100644 --- a/03函数/04匿名函数.php +++ b/03函数/04匿名函数.php @@ -4,6 +4,7 @@ * 与普通函数一样有返回值 * 可以作为一个变量值使用 * 常用于做回调函数的参数 + * 把匿名函数当成一个对象 */ //1 匿名函数作为变量值 @@ -26,22 +27,67 @@ //2 匿名函数作为函数里的函数 - function func(){ - $b='world'; + $b='父类中的变量'; - $innerFunc = function(){ - //调用函数func的局部变量 - echo $b; //wrong output:Undefined variable: b + //wrong output:Undefined variable: b + // $innerFunc = function(){ + // //调用函数func的局部变量 + // echo $b; + // }; + + //3 匿名函数调用父类中的变量 + $innerFunc = function() use ($b){ + echo "调用".$b.PHP_EOL; }; //调用匿名函数 $innerFunc(); } + func(); + - // //匿名函数作为参数,回调函数 - function mathOpt(function(){return 0;}){ + //匿名函数作为参数,回调函数 + //array_map(?callable $callback, array $array, array ...$arrays): array + //数组$array中的元素依次赋值到匿名函数中的$num + $arr=[1,2,3,4]; + $ret = array_map( + function($num){ + return $num * $num; + }, + $arr); + print_r($ret); + + + //静态匿名函数 + //匿名函数允许被定义为静态化。这样可以防止当前类自动绑定到它们身上,对象在运行时也可能不会被绑定到它们上面。 + //不是很理解这句话 + + //在类中的匿名函数,不带static + class ClassA + { + public $a = 3; + + function __construct() + { + $func = function(){ + echo $this->a.PHP_EOL; + }; + $func(); + } + } + + $obj=new ClassA(); + //带static的匿名函数 + class ClassB + { + public $a=4; + function __construct() { + $func=static function(){echo $this->a.PHP_EOL;}; //wrong output:Using $this when not in object context + $func(); + } } + $obj2=new ClassB(); ?> \ No newline at end of file diff --git a/03函数/08箭头函数.php b/03函数/08箭头函数.php new file mode 100644 index 0000000..6b89d52 --- /dev/null +++ b/03函数/08箭头函数.php @@ -0,0 +1,33 @@ +表达式 + */ + + //匿名函数 + $a = function($n){ + return $n*$n; + }; + + echo "使用匿名函数".$a(3).PHP_EOL; + + //箭头函数 + $b=fn($n) => $n*$n; + echo "使用箭头函数".$b(3).PHP_EOL; + + + //2 箭头函数不用use,会自动继承父类的变量 + $c=5; + + //匿名函数 + $fn1=function() use ($c){ + return $c * $c; + }; + + echo $fn1().PHP_EOL; + + //箭头函数 + $fn2=fn()=>$c*$c; //这里箭头函数不用形参,就能使用父类中的$c + echo $fn2().PHP_EOL; + +?> \ No newline at end of file diff --git a/07面向对象/05重载/__set.php b/07面向对象/05重载/__set.php index 8ac51ae..95b1f53 100644 --- a/07面向对象/05重载/__set.php +++ b/07面向对象/05重载/__set.php @@ -3,8 +3,9 @@ * 重载:重新写类成员,只有当调用类中没有的成员时才会使用 */ + //__set(), 未定义或不可访问的 class clsA{ - public $a = 'hello'; + public $a = 'hello'; private $b; public function __set($key,$val) { diff --git a/07面向对象/05重载/当调用未定义的变量或方式时触发 b/07面向对象/05重载/当调用未定义的变量或方式时触发 new file mode 100644 index 0000000..e69de29 diff --git a/07面向对象/06抽象类.php b/07面向对象/06抽象类abstract.php similarity index 78% rename from 07面向对象/06抽象类.php rename to 07面向对象/06抽象类abstract.php index 8986d08..19e5c17 100644 --- a/07面向对象/06抽象类.php +++ b/07面向对象/06抽象类abstract.php @@ -1,4 +1,9 @@ str.PHP_EOL; + + $obj2 = new classB(); + echo $obj2->str.PHP_EOL; //找不到文件./classB.php +?> \ No newline at end of file diff --git a/11自动加载类/classA.php b/07面向对象/11自动加载类/classA.php similarity index 100% rename from 11自动加载类/classA.php rename to 07面向对象/11自动加载类/classA.php diff --git a/07面向对象/11重载.php b/07面向对象/11重载.php deleted file mode 100644 index 69007d5..0000000 --- a/07面向对象/11重载.php +++ /dev/null @@ -1,3 +0,0 @@ - \ No newline at end of file diff --git a/07面向对象/13静态属性方法static.php b/07面向对象/13静态属性方法static.php new file mode 100644 index 0000000..d87c691 --- /dev/null +++ b/07面向对象/13静态属性方法static.php @@ -0,0 +1,31 @@ +和self:: + public static function fnB(){ + // echo $this->$varA; //this要通过对象来使用 + echo self::$varA; + } + } + + //访问静态属性 + echo ClassA::$varA.PHP_EOL; + + //访问静态方法 + ClassA::fnA(); + + //对比this->和self:: + ClassA::fnB(); +?> \ No newline at end of file diff --git a/07面向对象/14代码复用trait.php b/07面向对象/14代码复用trait.php new file mode 100644 index 0000000..3f2d40e --- /dev/null +++ b/07面向对象/14代码复用trait.php @@ -0,0 +1,20 @@ +open(); +?> \ No newline at end of file diff --git a/07面向对象/17遍历对象.php b/07面向对象/17遍历对象.php new file mode 100644 index 0000000..a354065 --- /dev/null +++ b/07面向对象/17遍历对象.php @@ -0,0 +1,27 @@ + $value){ + echo "$key:$value".PHP_EOL; + } +?> \ No newline at end of file diff --git a/07面向对象/18静态绑定.php b/07面向对象/18静态绑定.php new file mode 100644 index 0000000..649fb8f --- /dev/null +++ b/07面向对象/18静态绑定.php @@ -0,0 +1,40 @@ + \ No newline at end of file diff --git a/07面向对象/19对象的引用.php b/07面向对象/19对象的引用.php new file mode 100644 index 0000000..c8b1fb5 --- /dev/null +++ b/07面向对象/19对象的引用.php @@ -0,0 +1,26 @@ +var=2; + echo $a->var.PHP_EOL; + + $c=new ClassA; + $d=&$c; //引用 + $d->var=2; + echo $c->var.PHP_EOL; + + $e=new ClassA; + function func($obj){ + $obj->var=2; + } + func($e); + echo $e->var; +?> \ No newline at end of file diff --git a/07面向对象/20序列化对象-在会话中存放对象/classA.php b/07面向对象/20序列化对象-在会话中存放对象/classA.php new file mode 100644 index 0000000..167a111 --- /dev/null +++ b/07面向对象/20序列化对象-在会话中存放对象/classA.php @@ -0,0 +1,13 @@ +varA; + } + } +?> \ No newline at end of file diff --git a/07面向对象/20序列化对象-在会话中存放对象/page1.php b/07面向对象/20序列化对象-在会话中存放对象/page1.php new file mode 100644 index 0000000..919af1c --- /dev/null +++ b/07面向对象/20序列化对象-在会话中存放对象/page1.php @@ -0,0 +1,8 @@ + \ No newline at end of file diff --git a/07面向对象/20序列化对象-在会话中存放对象/page2.php b/07面向对象/20序列化对象-在会话中存放对象/page2.php new file mode 100644 index 0000000..7ab9c03 --- /dev/null +++ b/07面向对象/20序列化对象-在会话中存放对象/page2.php @@ -0,0 +1,8 @@ +funA(); +?> \ No newline at end of file diff --git a/07面向对象/20序列化对象-在会话中存放对象/store b/07面向对象/20序列化对象-在会话中存放对象/store new file mode 100644 index 0000000..8b5cd79 --- /dev/null +++ b/07面向对象/20序列化对象-在会话中存放对象/store @@ -0,0 +1 @@ +O:6:"ClassA":1:{s:4:"varA";i:1;} \ No newline at end of file diff --git a/07面向对象/21协变和逆变/01协变.php b/07面向对象/21协变和逆变/01协变.php new file mode 100644 index 0000000..34fd111 --- /dev/null +++ b/07面向对象/21协变和逆变/01协变.php @@ -0,0 +1,88 @@ +name=$name; + } + + abstract public function speak(); + } + + class Dog extends Animal{ + public function speak(){ + echo $this -> name." barks".PHP_EOL; + } + } + + class Cat extends Animal{ + public function speak(){ + echo $this->name." meows".PHP_EOL; + } + } + + $doggy=new Dog("旺财"); + $doggy->speak(); + + + + interface AnimalShelter{ + public function adopt(string $name):Animal; + } + + class CatShelter implements AnimalShelter{ + public function adopt(string $name):Cat{ + return new Cat($name); + } + } + + class DogShelter implements AnimalShelter{ + public function adopt(string $name):Dog{ + return new Dog($name); + } + } + + $kitty=(new CatShelter)->adopt("ricky"); + $kitty->speak(); + + $doggy=(new DogShelter)->adopt("lucky"); + $doggy->speak(); + + + class Food{} + + class AnimalFood extends Food{} + + abstract class Animal{ + protected string $name; + + public function __construct(string $name){ + $this->name=$name; + } + + public function eat(AnimalFood $food){ + echo $this->name."eats".get_class($food); + } + } + + class Dog extends Animal{ + public function eat(Food $food){ + echo $this->name."eats".get_class($food); + } + } + + $kitty=(new CatShelter)->adopt("ricky"); + $catFood=new AnimalFood(); + $kitty->eat($catFood).PHP_EOL; + + $doggy=(new DogShelter)->adopt("lucky"); + $banana=new Food(); + $doggy->eat($banana); + +?> \ No newline at end of file diff --git a/07面向对象/21协变和逆变/02逆变.php b/07面向对象/21协变和逆变/02逆变.php new file mode 100644 index 0000000..98830ef --- /dev/null +++ b/07面向对象/21协变和逆变/02逆变.php @@ -0,0 +1,32 @@ +name=$name; + } + + public function eat(AnimalFood $food){ + echo $this->name."eats".get_class($food); + } + } + + class Dog extends Animal{ + public function eat(Food $food){ + echo $this->name."eats".get_class($food); + } + } + + $kitty=(new CatShelter)->adopt("ricky"); + $catFood=new AnimalFood(); + $kitty->eat($catFood).PHP_EOL; + + $doggy=(new DogShelter)->adopt("lucky"); + $banana=new Food(); + $doggy->eat($banana); +?> \ No newline at end of file diff --git a/11自动加载类/autoload.php b/11自动加载类/autoload.php deleted file mode 100644 index 696df04..0000000 --- a/11自动加载类/autoload.php +++ /dev/null @@ -1,20 +0,0 @@ -str.'
'; - - $obj2 = new classB(); - echo $obj2->str.'
'; -?> \ No newline at end of file diff --git a/11自动加载类/classB.php b/11自动加载类/classB.php deleted file mode 100644 index a778d42..0000000 --- a/11自动加载类/classB.php +++ /dev/null @@ -1,6 +0,0 @@ - \ No newline at end of file diff --git a/16其他/01-php执行shell.php b/16其他/01-php执行shell.php new file mode 100644 index 0000000..0805153 --- /dev/null +++ b/16其他/01-php执行shell.php @@ -0,0 +1,9 @@ + \ No newline at end of file