diff --git a/07面向对象/01.php b/07面向对象/01.php deleted file mode 100644 index 7f2c6ce..0000000 --- a/07面向对象/01.php +++ /dev/null @@ -1,20 +0,0 @@ - \ No newline at end of file diff --git a/07面向对象/01继承.php b/07面向对象/01继承.php new file mode 100644 index 0000000..b58dbb5 --- /dev/null +++ b/07面向对象/01继承.php @@ -0,0 +1,96 @@ +'; + } + + public function demo() //public 成员 + { + echo '--父类 方法--'.'
'; + } + + public function __destruct() + { + echo '--推出父类--'.'
'; + } + } + + class son extends classParent + { + public function __construct() + { + echo '--进入子类--'.'
'; + } + + public function __destruct() + { + echo '--退出子类--'.'
'; + } + } + + // $obj = new son(); + // $obj -> demo(); + + + /*继承父类中的保存成员*/ + + class parent2 + { + public function __construct() + { + echo '--进入父类--'.'
'; + } + + protected function demo() + { + echo '父类保护成员'.'
'; + } + + public function __destruct() + { + echo '--退出父类--'.'
'; + } + } + + class son2 extends parent2{ + public function __construct() + { + echo '--进入子类--'.'
'; + } + + public function test() + { + echo '子类公共成员'.'
'; + $this -> demo(); //这个this是指子son2; + } + + public function __destruct() + { + echo '--退出子类--'.'
'; + } + } + + + // $obj = new son2(); + // $obj -> test(); + + /*继承父类中的私有成员*/ + class parent3{ + private function func(){ + echo '父类私有成员'.'
'; + } + } + + class son3 extends parent3{ + public function test(){ + $this -> func(); //会报错 + } + } + + $obj=new son3(); + $obj -> test(); +?> \ No newline at end of file diff --git a/07面向对象/02命名空间/01显示当前的命名空间.php b/07面向对象/02命名空间/01显示当前的命名空间.php new file mode 100644 index 0000000..3e1713e --- /dev/null +++ b/07面向对象/02命名空间/01显示当前的命名空间.php @@ -0,0 +1,6 @@ + \ No newline at end of file diff --git a/07面向对象/02命名空间/02使用命名空间/file1.php b/07面向对象/02命名空间/02使用命名空间/file1.php new file mode 100644 index 0000000..9460380 --- /dev/null +++ b/07面向对象/02命名空间/02使用命名空间/file1.php @@ -0,0 +1,18 @@ +'; + } + + class foo + { + // public function demo() { + //如果使用public,会报错Fatal error: Uncaught Error: Non-static method Foo\Bar\subnamespace\foo::demo() cannot be called statically in + static function demo() { + echo '命名空间为:'.__NAMESPACE__.'--类foo的方法demo'.'
'; + } + } +?> \ No newline at end of file diff --git a/07面向对象/02命名空间/02使用命名空间/file2.php b/07面向对象/02命名空间/02使用命名空间/file2.php new file mode 100644 index 0000000..4ee7846 --- /dev/null +++ b/07面向对象/02命名空间/02使用命名空间/file2.php @@ -0,0 +1,33 @@ +'; +} + +class foo +{ + static function demo() { + echo '命名空间为:'.__NAMESPACE__.'--类foo的方法demo'.'
'; + } +} + +/* 非限定名称 */ +foo(); // 解析为函数 Foo\Bar\foo +foo::demo(); // 解析为类 Foo\Bar\foo 的静态方法 staticmethod +echo '命名空间为:'.__NAMESPACE__.'--常量FOO:'.FOO.'
'; // 解析为常量 Foo\Bar\FOO + +/* 限定名称 */ +subnamespace\foo(); // 解析为函数 Foo\Bar\subnamespace\foo +subnamespace\foo::demo(); // 解析为类 Foo\Bar\subnamespace\foo, + // 以及类的方法 staticmethod +echo '命名空间为:'.__NAMESPACE__.'--常量FOO:'.subnamespace\FOO.'
'; // 解析为常量 Foo\Bar\subnamespace\FOO + +/* 完全限定名称 */ +\Foo\Bar\foo(); // 解析为函数 Foo\Bar\foo +\Foo\Bar\foo::demo(); // 解析为类 Foo\Bar\foo, 以及类的方法 staticmethod +echo \Foo\Bar\FOO; // 解析为常量 Foo\Bar\FOO +?> \ No newline at end of file diff --git a/07面向对象/02命名空间/03use的使用/demo.php b/07面向对象/02命名空间/03use的使用/demo.php new file mode 100644 index 0000000..8ab8f39 --- /dev/null +++ b/07面向对象/02命名空间/03use的使用/demo.php @@ -0,0 +1,54 @@ +'; + } + + class cls_a + { + static function func() + { + echo __CLASS__.'--'.__FUNCTION__.'
'; + } + } + } + + //命名空间b + namespace b + { + //导入类 + use a\cls_a as cls_b; //导入命名空间a\b\c的类cls_a,设置别名cls_a + + $obj=new cls_b; + $obj -> func(); + + //导入函数 + use function a\func_a; + func_a(); + + //导入常量 + use const a\const_a; + echo const_a.'
'; + } + + namespace c + { + //导入命名空间 + use a as aa; + + echo aa\const_a.'
';; + + aa\func_a(); + + $obj=new aa\cls_a(); + $obj -> func(); + } + + //感悟:命名空间就像在一个文件中把其中一筐代码框住,放在一个空间中,当要使用是就用use导入 + //注意:导入不同文件的命名空间需要用iclude'file' +?> \ No newline at end of file diff --git a/07面向对象/02命名空间/03use的使用/file.php b/07面向对象/02命名空间/03use的使用/file.php new file mode 100644 index 0000000..f39b0c7 --- /dev/null +++ b/07面向对象/02命名空间/03use的使用/file.php @@ -0,0 +1,18 @@ + \ No newline at end of file diff --git a/07面向对象/03工场模式--创建对象的类.php b/07面向对象/03工场模式--创建对象的类.php new file mode 100644 index 0000000..77ac09f --- /dev/null +++ b/07面向对象/03工场模式--创建对象的类.php @@ -0,0 +1,59 @@ + getVal(5,7); + echo $ret; +?> \ No newline at end of file diff --git a/07面向对象/04单例模式.php b/07面向对象/04单例模式.php new file mode 100644 index 0000000..69007d5 --- /dev/null +++ b/07面向对象/04单例模式.php @@ -0,0 +1,3 @@ + \ No newline at end of file diff --git a/07面向对象/05魔术方法/__get.php b/07面向对象/05魔术方法/__get.php new file mode 100644 index 0000000..2a3b51c --- /dev/null +++ b/07面向对象/05魔术方法/__get.php @@ -0,0 +1,14 @@ +'; + } + } + + $obj = new clsA(); + $obj -> a = 'world'; + echo $obj -> b; //b未定义,当b被调用时,自动调用了__get,自动传入key +?> \ No newline at end of file diff --git a/07面向对象/05魔术方法/set.php b/07面向对象/05魔术方法/set.php new file mode 100644 index 0000000..070e1f6 --- /dev/null +++ b/07面向对象/05魔术方法/set.php @@ -0,0 +1,14 @@ +'; + } + } + + $obj = new clsA(); + $obj -> a = 'world'; + $obj -> b = 'test'; //b未定义,但b被赋值时,自动调用了__set,自动传入b和test +?> \ No newline at end of file diff --git a/07面向对象/06抽象类.php b/07面向对象/06抽象类.php new file mode 100644 index 0000000..8986d08 --- /dev/null +++ b/07面向对象/06抽象类.php @@ -0,0 +1,22 @@ +'; + } + } + + $adog = new dog(); + $adog -> eat(); + + //注意:抽象类不能被实例化,已经要被子类继承 +?> \ No newline at end of file diff --git a/07面向对象/07接口.php b/07面向对象/07接口.php new file mode 100644 index 0000000..535d9b1 --- /dev/null +++ b/07面向对象/07接口.php @@ -0,0 +1,39 @@ +'; + } + + function run() + { + echo "狗跑去操场".'
'; + } + } + + $adog = new dog(); + $adog -> eat(); + $adog -> run(); +?> \ No newline at end of file diff --git a/07面向对象/08最终类final.php b/07面向对象/08最终类final.php new file mode 100644 index 0000000..a43e724 --- /dev/null +++ b/07面向对象/08最终类final.php @@ -0,0 +1,30 @@ +'; + } + } + + class dog extends animal + { + public function eat() //应该会报错 Cannot override final method animal::eat() + { + echo "dog eat bone".'
'; + } + } +?> \ No newline at end of file diff --git a/07面向对象/09克隆clone.php b/07面向对象/09克隆clone.php new file mode 100644 index 0000000..61e2be6 --- /dev/null +++ b/07面向对象/09克隆clone.php @@ -0,0 +1,15 @@ +color.'
'; + } + } + + $adog = new dog(); + $anthorDog = clone $adog; + + $adog->show(); + $anthorDog->show(); +?> \ No newline at end of file diff --git a/07面向对象/10判断对象是否属于某个类.php b/07面向对象/10判断对象是否属于某个类.php new file mode 100644 index 0000000..1b9de9a --- /dev/null +++ b/07面向对象/10判断对象是否属于某个类.php @@ -0,0 +1,17 @@ +'; + if($obj instanceof B){ + echo 'true'.'
'; + }else{ + echo 'false'.'
'; + } + + $ret=$obj instanceof A; + echo $ret.'
'; +?> \ No newline at end of file