分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 前端开发

php面向对象补充

发布时间:2023-09-06 01:06责任编辑:彭小芳关键词:面向对象

面向对象补充

__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();

php面向对象补充

原文地址:http://www.cnblogs.com/sunzhenkun/p/7440781.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved