2020年5月22日

    1. <?php
    2. class A{
    3. public $name='黑子';
    4. public function __toString(){
    5. //这里面返回的字符串可以自己随意拼接
    6. return $this->name;
    7. }
    8. }
    9. $obj=new A();
    10. echo $objl
    11. echo"<br>";
    12. class B{
    13. protected function test(){}
    14. //$name =>方法名
    15. //$param=>方法传递的所有参数(数组格式)
    16. public function __call($name,$param){
    17. var_dump($name);
    18. var_dump($param);
    19. }
    20. }
    21. $obj2=new B();
    22. //调用未定义的方法;
    23. $obj2->demo(10);
    24. //调用不可访问的方法
    25. $obj2->test('test');
    26. class Database{
    27. private static $obj =null;
    28. public static function getobj(){
    29. if(self::$obj ===null){
    30. self::$obj = new self();
    31. }
    32. return self::$obj;
    33. }
    34. private function __construct(){
    35. $this->connect();
    36. }
    37. public function connect()
    38. {
    39. var_dump('connecting database');
    40. }
    41. }
    42. $obj1 =Database::getobj();
    43. var_dump($obj1);
    44. $obj2 =Database::getobj();
    45. var_dump($obj2);
    46. $obj3 =Database::getobj();
    47. var_dump($obj3);
    48. class A{
    49. public $name = "jp";
    50. public $age =18
    51. public $buer = true;
    52. public function __construct(){}
    53. }
    54. $obj1 =new A();
    55. var_dump($obj1);
    56. echo"<hr>";
    57. echo serialize($obj1);
    58. echo "<hr>";
    59. $str ='0:1"A":3:{s:4:"name";s:4:"obj1";s:3:"age";i:18;s:4:"buer";b:1;}';
    60. $obj=unserialize($str);
    61. var_dump($obj);
    62. class File{
    63. public $file;
    64. public $filename;
    65. public $bool =true;
    66. public function __construct($name){
    67. $this->filename =$name;
    68. $this->open();
    69. }
    70. publiuc function open(){
    71. $this->file='打开文件:' .$this->filename;
    72. }
    73. public function __sleep(){
    74. return['filename'];
    75. }
    76. public function __wakeup(){
    77. $this->open();
    78. }
    79. }
    80. $obj1=new File('./哈哈哈.txt');
    81. var_dump($obj1);
    82. echo"<hr>";
    83. echo serialize($obj1);
    84. echo"<hr>";
    85. $str='0:4:"File":1:{s:8:"filename";s:18:"./哈哈哈.txt";}';
    86. $obj2=unserialize($str);
    87. var_dump($obj2);
    88. <?php
    89. class A{
    90. public $name='黑子';
    91. public function __toString(){
    92. //这里面返回的字符串可以自己随意拼接
    93. return $this->name;
    94. }
    95. }
    96. $obj=new A();
    97. echo $objl
    98. echo"<br>";
    99. class B{
    100. protected function test(){}
    101. //$name =>方法名
    102. //$param=>方法传递的所有参数(数组格式)
    103. public function __call($name,$param){
    104. var_dump($name);
    105. var_dump($param);
    106. }
    107. }
    108. $obj2=new B();
    109. //调用未定义的方法;
    110. $obj2->demo(10);
    111. //调用不可访问的方法
    112. $obj2->test('test');
    113. class Database{
    114. private static $obj =null;
    115. public static function getobj(){
    116. if(self::$obj ===null){
    117. self::$obj = new self();
    118. }
    119. return self::$obj;
    120. }
    121. private function __construct(){
    122. $this->connect();
    123. }
    124. public function connect()
    125. {
    126. var_dump('connecting database');
    127. }
    128. }
    129. $obj1 =Database::getobj();
    130. var_dump($obj1);
    131. $obj2 =Database::getobj();
    132. var_dump($obj2);
    133. $obj3 =Database::getobj();
    134. var_dump($obj3);
    135. class A{
    136. public $name = "jp";
    137. public $age =18
    138. public $buer = true;
    139. public function __construct(){}
    140. }
    141. $obj1 =new A();
    142. var_dump($obj1);
    143. echo"<hr>";
    144. echo serialize($obj1);
    145. echo "<hr>";
    146. $str ='0:1"A":3:{s:4:"name";s:4:"obj1";s:3:"age";i:18;s:4:"buer";b:1;}';
    147. $obj=unserialize($str);
    148. var_dump($obj);
    149. class File{
    150. public $file;
    151. public $filename;
    152. public $bool =true;
    153. public function __construct($name){
    154. $this->filename =$name;
    155. $this->open();
    156. }
    157. publiuc function open(){
    158. $this->file='打开文件:' .$this->filename;
    159. }
    160. public function __sleep(){
    161. return['filename'];
    162. }
    163. public function __wakeup(){
    164. $this->open();
    165. }
    166. }
    167. $obj1=new File('./哈哈哈.txt');
    168. var_dump($obj1);
    169. echo"<hr>";
    170. echo serialize($obj1);
    171. echo"<hr>";
    172. $str='0:4:"File":1:{s:8:"filename";s:18:"./哈哈哈.txt";}';
    173. $obj2=unserialize($str);
    174. var_dump($obj2);
    175. <?php
    176. class A{
    177. public $name='黑子';
    178. public function __toString(){
    179. //这里面返回的字符串可以自己随意拼接
    180. return $this->name;
    181. }
    182. }
    183. $obj=new A();
    184. echo $objl
    185. echo"<br>";
    186. class B{
    187. protected function test(){}
    188. //$name =>方法名
    189. //$param=>方法传递的所有参数(数组格式)
    190. public function __call($name,$param){
    191. var_dump($name);
    192. var_dump($param);
    193. }
    194. }
    195. $obj2=new B();
    196. //调用未定义的方法;
    197. $obj2->demo(10);
    198. //调用不可访问的方法
    199. $obj2->test('test');
    200. class Database{
    201. private static $obj =null;
    202. public static function getobj(){
    203. if(self::$obj ===null){
    204. self::$obj = new self();
    205. }
    206. return self::$obj;
    207. }
    208. private function __construct(){
    209. $this->connect();
    210. }
    211. public function connect()
    212. {
    213. var_dump('connecting database');
    214. }
    215. }
    216. $obj1 =Database::getobj();
    217. var_dump($obj1);
    218. $obj2 =Database::getobj();
    219. var_dump($obj2);
    220. $obj3 =Database::getobj();
    221. var_dump($obj3);
    222. class A{
    223. public $name = "jp";
    224. public $age =18
    225. public $buer = true;
    226. public function __construct(){}
    227. }
    228. $obj1 =new A();
    229. var_dump($obj1);
    230. echo"<hr>";
    231. echo serialize($obj1);
    232. echo "<hr>";
    233. $str ='0:1"A":3:{s:4:"name";s:4:"obj1";s:3:"age";i:18;s:4:"buer";b:1;}';
    234. $obj=unserialize($str);
    235. var_dump($obj);
    236. class File{
    237. public $file;
    238. public $filename;
    239. public $bool =true;
    240. public function __construct($name){
    241. $this->filename =$name;
    242. $this->open();
    243. }
    244. publiuc function open(){
    245. $this->file='打开文件:' .$this->filename;
    246. }
    247. public function __sleep(){
    248. return['filename'];
    249. }
    250. public function __wakeup(){
    251. $this->open();
    252. }
    253. }
    254. $obj1=new File('./哈哈哈.txt');
    255. var_dump($obj1);
    256. echo"<hr>";
    257. echo serialize($obj1);
    258. echo"<hr>";
    259. $str='0:4:"File":1:{s:8:"filename";s:18:"./哈哈哈.txt";}';
    260. $obj2=unserialize($str);
    261. var_dump($obj2);

    笔记:
    魔术方法 toString()
    toString(void):string
    当对象被当成是字符串去使用时自动调用,并且该方法必须返回一个字符串

    魔术方法 call
    当调用一个不存在或不可访问的方法时,php会报错
    当写
    call方法时,报错将交由call来处理
    call(string $funcName,arry $args) : mixed

    单态设计模式=>单态套路
    一个类只允许出现一次实例化(对象)

    对象的串行化和反串行
    将对象持久化的储存或者传输,转换为字符串;
    串行化 =>对象变成字符串(特定格式)
    反串化 =>字符串转换为对象

    串行化 serialize(objec $obj):string
    反串化unserialize(string $str): mixed

    魔术方法:
    sleep(void):array
    返回需要保存的属性名字,在对一个对象进行串行化时执行
    wakeup(void):void
    针对一些无法保存的属性进行初始化,在反串行化时执行
    资源(mysql的连接 打开的文件 打开目录)