php面向对象补充

面向对象补充

__tostring的方法

class Ren{
    public $name;
    public function __tostring(){
        return "这是人类,有成员变量name代表名称";
    }
}
$r = new Ren();
echo $r;

自动调用tostring方法。

 

克隆对象的方法

class Ren{
    public $name;
    
    public function __tostring(){
        return "这是Ren类,有成员变量name代表名称";
    }
    //当克隆对象的时候,自动调用
    public function __clone(){
        $this->name = "李四"; //$this代表复本
    }
}
$r = new Ren();
$r->name = "张三";
//echo $r; //自动调用tostring方法
var_dump($r);

$r1 = clone $r;
var_dump($r1);

 

加载类

include("./Test.class.php");
include "./Test.class.php";
include_once("./Test.class.php");
include_once "./Test.class.php";

require("./Test.class.php");
require "./Test.class.php";
require_once("./Test.class.php");
require_once "./Test.class.php";

自动加载类
1.类名必须和文件名保持一致
2.所有类的命名规则一致
3.所有类必须在同一个文件夹下

function __autoload($cname){
    require_once "./{$cname}.class.php";
}

$t = new Test();
$t->ceshi();
$r = new Ren();
$r->test();

 

转载于:https://www.cnblogs.com/sunzhenkun/p/7440781.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值