2020年5月22日
<?php
class A{
public $name='黑子';
public function __toString(){
//这里面返回的字符串可以自己随意拼接
return $this->name;
}
}
$obj=new A();
echo $objl
echo"<br>";
class B{
protected function test(){}
//$name =>方法名
//$param=>方法传递的所有参数(数组格式)
public function __call($name,$param){
var_dump($name);
var_dump($param);
}
}
$obj2=new B();
//调用未定义的方法;
$obj2->demo(10);
//调用不可访问的方法
$obj2->test('test');
class Database{
private static $obj =null;
public static function getobj(){
if(self::$obj ===null){
self::$obj = new self();
}
return self::$obj;
}
private function __construct(){
$this->connect();
}
public function connect()
{
var_dump('connecting database');
}
}
$obj1 =Database::getobj();
var_dump($obj1);
$obj2 =Database::getobj();
var_dump($obj2);
$obj3 =Database::getobj();
var_dump($obj3);
class A{
public $name = "jp";
public $age =18
public $buer = true;
public function __construct(){}
}
$obj1 =new A();
var_dump($obj1);
echo"<hr>";
echo serialize($obj1);
echo "<hr>";
$str ='0:1"A":3:{s:4:"name";s:4:"obj1";s:3:"age";i:18;s:4:"buer";b:1;}';
$obj=unserialize($str);
var_dump($obj);
class File{
public $file;
public $filename;
public $bool =true;
public function __construct($name){
$this->filename =$name;
$this->open();
}
publiuc function open(){
$this->file='打开文件:' .$this->filename;
}
public function __sleep(){
return['filename'];
}
public function __wakeup(){
$this->open();
}
}
$obj1=new File('./哈哈哈.txt');
var_dump($obj1);
echo"<hr>";
echo serialize($obj1);
echo"<hr>";
$str='0:4:"File":1:{s:8:"filename";s:18:"./哈哈哈.txt";}';
$obj2=unserialize($str);
var_dump($obj2);
<?php
class A{
public $name='黑子';
public function __toString(){
//这里面返回的字符串可以自己随意拼接
return $this->name;
}
}
$obj=new A();
echo $objl
echo"<br>";
class B{
protected function test(){}
//$name =>方法名
//$param=>方法传递的所有参数(数组格式)
public function __call($name,$param){
var_dump($name);
var_dump($param);
}
}
$obj2=new B();
//调用未定义的方法;
$obj2->demo(10);
//调用不可访问的方法
$obj2->test('test');
class Database{
private static $obj =null;
public static function getobj(){
if(self::$obj ===null){
self::$obj = new self();
}
return self::$obj;
}
private function __construct(){
$this->connect();
}
public function connect()
{
var_dump('connecting database');
}
}
$obj1 =Database::getobj();
var_dump($obj1);
$obj2 =Database::getobj();
var_dump($obj2);
$obj3 =Database::getobj();
var_dump($obj3);
class A{
public $name = "jp";
public $age =18
public $buer = true;
public function __construct(){}
}
$obj1 =new A();
var_dump($obj1);
echo"<hr>";
echo serialize($obj1);
echo "<hr>";
$str ='0:1"A":3:{s:4:"name";s:4:"obj1";s:3:"age";i:18;s:4:"buer";b:1;}';
$obj=unserialize($str);
var_dump($obj);
class File{
public $file;
public $filename;
public $bool =true;
public function __construct($name){
$this->filename =$name;
$this->open();
}
publiuc function open(){
$this->file='打开文件:' .$this->filename;
}
public function __sleep(){
return['filename'];
}
public function __wakeup(){
$this->open();
}
}
$obj1=new File('./哈哈哈.txt');
var_dump($obj1);
echo"<hr>";
echo serialize($obj1);
echo"<hr>";
$str='0:4:"File":1:{s:8:"filename";s:18:"./哈哈哈.txt";}';
$obj2=unserialize($str);
var_dump($obj2);
<?php
class A{
public $name='黑子';
public function __toString(){
//这里面返回的字符串可以自己随意拼接
return $this->name;
}
}
$obj=new A();
echo $objl
echo"<br>";
class B{
protected function test(){}
//$name =>方法名
//$param=>方法传递的所有参数(数组格式)
public function __call($name,$param){
var_dump($name);
var_dump($param);
}
}
$obj2=new B();
//调用未定义的方法;
$obj2->demo(10);
//调用不可访问的方法
$obj2->test('test');
class Database{
private static $obj =null;
public static function getobj(){
if(self::$obj ===null){
self::$obj = new self();
}
return self::$obj;
}
private function __construct(){
$this->connect();
}
public function connect()
{
var_dump('connecting database');
}
}
$obj1 =Database::getobj();
var_dump($obj1);
$obj2 =Database::getobj();
var_dump($obj2);
$obj3 =Database::getobj();
var_dump($obj3);
class A{
public $name = "jp";
public $age =18
public $buer = true;
public function __construct(){}
}
$obj1 =new A();
var_dump($obj1);
echo"<hr>";
echo serialize($obj1);
echo "<hr>";
$str ='0:1"A":3:{s:4:"name";s:4:"obj1";s:3:"age";i:18;s:4:"buer";b:1;}';
$obj=unserialize($str);
var_dump($obj);
class File{
public $file;
public $filename;
public $bool =true;
public function __construct($name){
$this->filename =$name;
$this->open();
}
publiuc function open(){
$this->file='打开文件:' .$this->filename;
}
public function __sleep(){
return['filename'];
}
public function __wakeup(){
$this->open();
}
}
$obj1=new File('./哈哈哈.txt');
var_dump($obj1);
echo"<hr>";
echo serialize($obj1);
echo"<hr>";
$str='0:4:"File":1:{s:8:"filename";s:18:"./哈哈哈.txt";}';
$obj2=unserialize($str);
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的连接 打开的文件 打开目录)