欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于PHP的加载类操作以及其他两种魔术方法的应用实例

程序员文章站 2024-01-06 08:44:22
实例如下所示:

实例如下所示:

<?php 

加载类
//include("./ren.class.php");
//include "./ren.class.php";
include_once("./ren.class.php");
include_once("./ren.class.php");
$f = new ren();
$f->test();

require("./ren.class.php");
require_once("./ren.class.php");
require_once "./ren.class.php";
$f = new ren();
$f->test(); 

自动加载类
//1.所有类文件名和类名要保持一致
//2.所有类文件放在同一文件下
//3.所有类文件命名规则一致
function __autoload($cname){
 require_once("./$cname.class.php"); 
}
$t = new test();
$t->ceshi().'<br>';
$s = new ren();
$s->test();


两种魔术方法
 class ren{
 public $name;
 public function say(){
  echo "输出对象方法"; 
 }
 //输出对象的方法
 public function __tostring(){
  echo "另一种输出方法"; //echo $s->__tostring();
  return "另一种输出方法";//echo $s;
 }
 //克隆对象的方法
 public function __clone(){
  $this->name = "riven";//$this代表复本(克隆的对象)
 }
} 
$s = new ren();
//echo $s->__tostring();//输出字符串
//$s->say();

$s->name = "小v";
var_dump($s);

$s1 = clone $s;
var_dump($s1);
?>

以上这篇基于php的加载类操作以及其他两种魔术方法的应用实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

上一篇:

下一篇: