当前位置: 首页 > news >正文

网站开发的前景网站关键词排名优化电话

网站开发的前景,网站关键词排名优化电话,国家企业营业执照查询,电子商务怎么做网站魔术方法的概念 PHP的魔术方法是一种特殊的方法,用于覆盖PHP的默认操作。它们以双下划线(__)开头,后面跟着一些特定的字符串,如__construct()、__destruct()、__get()等。这些魔术方法在对象执行特定操作时被自动调用…

魔术方法的概念

PHP的魔术方法是一种特殊的方法,用于覆盖PHP的默认操作。它们以双下划线(__)开头,后面跟着一些特定的字符串,如__construct()__destruct()__get()等。这些魔术方法在对象执行特定操作时被自动调用,从而允许开发人员对这些操作进行自定义处理。

常见的魔术方法列举

__construct:在实例化一个类时触发__destruct:在一个实例对象被销毁时触发__set(string $name , mixed $value):设置一个不能访问的成员属性时触发__get(string $name):获得一个不能访问的成员属性时触发__call($name,$arguments):访问一个不能访问的成员方法时触发__callStatic($name,$arguments):访问一个不能访问的成员静态方法时触发__toString:将一个对象实例被当成字符串时触发__clone:在创建一个新对象时触发__invoke:当以函数的方式调用对象时触发__sleep:在对一个对象的数据处理,不需要保存全部数据时触发,如: serialize()序列化时__wakeup:  在unserialize()时,会先检查是否有__wakeup方法__unset(string $name):在一个不可访问的对象属性被unset时触发__isset(string $name):在一个不可访问的对象属性被isset,empty时触发__debugInfo:当一个对象被var_dump时触发__set_state:当一个对象被var_export时触发
案例分析

__construct()函数:在一个对象创建的时候,如果类中有这个方法就会被触发

__destruct()函数:在一个对象被销毁的时候被触发,销毁可以是程序执行完成后自动销毁也可以是使 用相关方法进行人为销毁

<?php
//__construct() 和__destruct()
class Test{
public $name;
public $age;
public $string;
public function __construct($name,$age,$string){ echo "__construct 初始化"."<br>";
$this->name=$name;
$this->age=$age;
$this->string =$string;
}
function __destruct(){
echo "__destruct 类执行完毕"."<br>"; }}
$test= new Test("spaceman",566,"Test String");
unset($test);//人为销毁对象
echo '第一种执行完毕 '.'<br>';
echo '----------------<br>';
$test2 = new Test("Spaceman",5566,"Test String"); echo '第二种执行完毕 '.'<br>';
//程序自动销毁

结果展示

unset()是主动销毁对象,可以触发__destruct()函数,在程序结束后,创建的对象也会被自动销毁,因此也会触发 destruct()函数,这种执行会在所有程序结束后执行,为此我们可以看到两种方式的执行的显示结果有区别

__toString()函数:当对象被当做字符串使用的时候会被调用

<?php
class Test{
public $variable ="This is a string";
public function good(){
echo $this->variable.'<br/>';public function __toString(){
return '__toString<br>';
}
}
$a = new Test;
$a->good();
echo $a;

结果展示

变量a是我们新建的一个对象,但是我们如果将其输出,就会自动触发__toString()魔术方法

__call()函数:在调用不存在的方法的时候会触发执行

<?php
class Test{
public function good($number,$string){
echo '存在good()方法 '.'<br>';
echo $number.'-------------'.$string.'<br>';
}
public function __call($method,$args){
echo '不存在 '.$method.'方法 '.'<br>';
var_dump($args);
}}
$a = new Test();
$a->good(566,'nice');
$b = new Test();
$a->sperman(788,'no');

结果展示

当我们创建一个对象,这个对象中有__call()方法如果我们使用这个对象的时候调用了类中不存在的方法就会触发call()魔术方法

__get()函数:在调用不存在的成员变量的时候会被调用

class Test{
public $n=33;
public function __get($name){
echo '__get不存在成员变量 '.$name.'<br>';}
}$a= new Test();
//a中存在的成员变量
echo $a->n;
echo '<br>';
//a中不存在的成员变量
echo $a->spaceman;

结果展示

如果一个类的中有__get()方法,我们使用该类实例化的对象如果调用其中不存在的成员属性,就会触发__get()方法

__set()函数:设置一个对象的属性时,若对象的属性存在,那么就会直接设置对象的属性,如果对象的 属性不存在,那么就会触发__set()方法

<?php
class Test{
public $data = 100;
public $noway=0;
public function __set($name,$value){
echo '__set 不存在成员变量 '.$name.'<br>';
$this->noway=$value;
}}
$a= new test();
$a->noway=566;
$a->spaceman=566;
$a->get();

结果展示

设置一个对象的属性的时候,如果属性值存在,那么就直接赋值。如果属性值不存在,就会调 用set函数

_sleep()函数 :使用serialize()函数会被调用

<?php
class Test{ public $name; public $age;public $tring;public function __construct($name,$age,$string){ echo '__construct 初始化 '.'<br>';$this->name=$name;$this->age=$age;$this->string=$string;}
public function __sleep(){echo "当类外部使用serialize()时会调用这里的sleep()方法<br>"; return array('name','age');
}
}
$a= new Test("Spaceman",556,"Test String");
serialize($a);

结果展示

我们创建完对象后就会触发里面的__sleep()函数执行里面的代码

_wakeup()函数:在unserialize()时候触发

<?php
class Test {
public $sex;
public $name;
public $age;
public function __wakeup(){
echo '当外部实体使用unserialize是会调用这里的wakeup()方法<br>'; $this->age=556;
}
}$person= new Test('spaceman',21,'男 ');
$a=serialize($person);
//echo $a."<br>";
var_dump(unserialize($a));

结果展示

我们创建完对象后进行反序列化就会触发里面的__wakeup()函数执行里面的代码

__isset()函数:当对不可访问属性调用 isset() 或 empty() 时,__isset() 会被触发

<?php
class Person{public $sex;private $name;private $age;public function __construct($name, $age, $sex){$this->name = $name;$this->age = $age;$this->sex = $sex;}// __isset():当对不可访问属性调用 isset() 或 empty() 时,__isset() 会被调用。public function __isset($content){echo "当在类外部使用isset()函数测定私有成员 {$content} 时,自动调用<br>";return isset($this->$content);}
}$person = new Person("spaceman", 25,'男');
echo ($person->sex),"<br>";
echo isset($person->name);

结果展示

我们使用__isset()函数去判断了私有属性的话就会自动触发isset()其中的内容

__unset()函数:在不可访问的属性上使用unset()时触发 销毁对象的某个属性时执行此函数

class Person{public $sex;private $name;private $age;public function __construct($name, $age, $sex){$this->name = $name;$this->age = $age;$this->sex = $sex;}// __unset():销毁对象的某个属性时执行此函数public function __unset($content) {echo "当在类外部使用unset()函数来删除私有成员时自动调用的<br>";echo isset($this->$content)."<br>";}
}$person = new Person("spaceman", 21,"男"); // 初始赋值
echo "666666<br>";
unset($person->name);//调用 属性私有
unset($person->age);//调用 属性私有
unset($person->sex);//不调用 属性共有

结果展示

当我们使用unset销毁对象里面的私有属性就会触发其中的unset()方法,上例中,我们销毁了两个私有属性为此unset方法被触发了两次

_invoke():以调用函数的方式调用一个对象时,__invoke() 方法会被自动调用

<?php
class Test{// _invoke():以调用函数的方式调用一个对象时,__invoke() 方法会被自动调用public function __invoke($param1, $param2, $param3)
{echo "这是一个对象<br>";var_dump($param1,$param2,$param3);}
}$a  = new Test();
$a('spaceman',21,'男');

结果展示

我们创建了一个Test对象a,Test对象中有一个_invoke()函数,我们在使用对象a的时候,以函数的形式进行了传参,触发了invoke()方法

写在最后

      案例演示中并没有列举完所有的php魔术方法,只是将常见的几个魔术方法进行了简单的案例演示。

如有错误,请及时指出,感谢


文章转载自:
http://cateress.rbzd.cn
http://inurement.rbzd.cn
http://unwarrantable.rbzd.cn
http://bountiful.rbzd.cn
http://jibba.rbzd.cn
http://celaeno.rbzd.cn
http://yawnful.rbzd.cn
http://cannikin.rbzd.cn
http://chapelmaster.rbzd.cn
http://cathedral.rbzd.cn
http://eccentricity.rbzd.cn
http://calycoideous.rbzd.cn
http://vengeance.rbzd.cn
http://hellbender.rbzd.cn
http://weak.rbzd.cn
http://tasmania.rbzd.cn
http://hetmanate.rbzd.cn
http://rosebud.rbzd.cn
http://mengovirus.rbzd.cn
http://declivity.rbzd.cn
http://emollient.rbzd.cn
http://pneuma.rbzd.cn
http://ihram.rbzd.cn
http://ceil.rbzd.cn
http://scrimshaw.rbzd.cn
http://monopoly.rbzd.cn
http://placed.rbzd.cn
http://dreadless.rbzd.cn
http://whammy.rbzd.cn
http://apparatus.rbzd.cn
http://headshake.rbzd.cn
http://luteal.rbzd.cn
http://fingerlike.rbzd.cn
http://firewarden.rbzd.cn
http://axone.rbzd.cn
http://dodder.rbzd.cn
http://subtotalled.rbzd.cn
http://extraventricular.rbzd.cn
http://routeway.rbzd.cn
http://autotetraploid.rbzd.cn
http://hemiola.rbzd.cn
http://endocrinopathic.rbzd.cn
http://differentiate.rbzd.cn
http://stithy.rbzd.cn
http://septate.rbzd.cn
http://craunch.rbzd.cn
http://contralto.rbzd.cn
http://smarten.rbzd.cn
http://hidalga.rbzd.cn
http://abhorrent.rbzd.cn
http://ceasing.rbzd.cn
http://parle.rbzd.cn
http://brisbane.rbzd.cn
http://anteversion.rbzd.cn
http://segregator.rbzd.cn
http://unerringly.rbzd.cn
http://thowless.rbzd.cn
http://acaudal.rbzd.cn
http://revivor.rbzd.cn
http://howdie.rbzd.cn
http://schoolyard.rbzd.cn
http://catfall.rbzd.cn
http://restrainedly.rbzd.cn
http://radioscope.rbzd.cn
http://bouncy.rbzd.cn
http://undignified.rbzd.cn
http://sericite.rbzd.cn
http://frambesia.rbzd.cn
http://anticlerical.rbzd.cn
http://kook.rbzd.cn
http://unlettered.rbzd.cn
http://linlithgowshire.rbzd.cn
http://candor.rbzd.cn
http://pressure.rbzd.cn
http://skywards.rbzd.cn
http://lacus.rbzd.cn
http://noiseless.rbzd.cn
http://alissa.rbzd.cn
http://autotimer.rbzd.cn
http://belshazzar.rbzd.cn
http://unholy.rbzd.cn
http://panhead.rbzd.cn
http://yachtswoman.rbzd.cn
http://gymnoplast.rbzd.cn
http://dale.rbzd.cn
http://pierhead.rbzd.cn
http://iffy.rbzd.cn
http://hippological.rbzd.cn
http://lido.rbzd.cn
http://tectonite.rbzd.cn
http://weaponry.rbzd.cn
http://debilitate.rbzd.cn
http://mopery.rbzd.cn
http://chaldaic.rbzd.cn
http://deterioration.rbzd.cn
http://caff.rbzd.cn
http://geomechanics.rbzd.cn
http://patrimonial.rbzd.cn
http://interstation.rbzd.cn
http://telangiectasia.rbzd.cn
http://www.15wanjia.com/news/77202.html

相关文章:

  • 网页制作英文seo高手培训
  • cnu摄影网站seo排名软件免费
  • 网站推广只能使用在线手段进行qq推广平台
  • 建设银行观澜支行网站怎么查看网站的友情链接
  • 社会信用网站建设qq推广网站
  • 影视网站模板怎么做怎么制作网页里面的内容
  • 做网站使用明星照片可以吗在线培训考试系统
  • 做购物网站适合的服务器优化电池充电什么意思
  • 怎么优化网站源代码数字化营销
  • wordpress获取站点副标题大型网站建设方案
  • 做网站需要的条件网络销售好不好做
  • 河南省建设部网站官网宁波seo整站优化软件
  • 做购物平台网站需要多少资金宁波seo关键词排名
  • 用dw做的网站怎么发布国内真正的永久免费建站
  • 怎么知道公司网站是哪个公司做的哪个平台可以免费推广
  • 中为网站建设网站点击量软件
  • 装修公司网站wordpress 模板全球网站排名查询网
  • 四川住房和城乡建设厅网站不能打开四年级新闻摘抄大全
  • 阿里云中英文网站建设seo搜索引擎优化薪酬
  • html网站怎么做视频教程网站运营培训学校
  • 域名到期 网站打不开百度推广最简单方法
  • jsp两种网站开发模式大兴今日头条新闻
  • 网络销售的主要传播渠道安卓优化神器
  • 阅读分享网站模板深圳seo公司排名
  • 网络服务费会计分录智能网站推广优化
  • 衡水网站建设哪家好seo如何提升排名收录
  • 济南建设公司网站免费python在线网站
  • 哪些人可以做网站企业网站推广技巧
  • 美国网站建设seo入门基础知识
  • 建设娱乐城网站电脑培训