自动加载类

_tostring()方法

class ren
{
  public $name;

  public function __tostring()
  {
    return "name代表姓名";
  }
}
$r=new ren();
echo $r;

这个方法要有返回值,写在类里,输出对象的引用

克隆对象

class ren
{
  public $name="张三";
  public function __clone()

  {

    $this->name="李四"

  }
}
$r=new ren();
$c=clone $r;
echo $c->name;

echo $c->name="李四";如果修改的时候获取的是修改的内容

加载类

首先再建一个php文件建在同一个文件夹下然后获取

如图:

include("./ren.class.php");
include "./ren.class.php";将全部内容加载的时候一个出错当前页面也出错
require("./ren.class.php");只加载相关的内容不考虑重复
require "./ren.class.php";
require_once("./ren.class.php");这个只能引用一次可防止多次引用
require_once "./ren.class.php";

自动加载类:这个可用于加载很多的类文件的时候

1.所有的类文件写在同一个目录下
2.类文件的命名规则保持一致
3.累的文件名要和类名保持一致

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

$r=new ren();
echo $r->name;

原文地址:https://www.cnblogs.com/douchenchen/p/6739022.html