数组换位置一.介绍 PHP(外文名:PHP: Hyperte(23)
做法1:使用__autoload魔术函数
<?php
function testFile(){
mkdir("C:/Users/Administrator/library");
$file = fopen("C:/Users/Administrator/library/A.class.php", ‘w‘);
fwrite($file, "<?php\n");
fwrite($file, "class A{}");
fclose($file);
}
function __autoload($className){
require "C:/Users/Administrator/library/{$className}.class.php";
}
testFile();
$obj = new A();
var_dump($obj);
做法2:使用spl_autoload_register函数
用spl_autoload_register函数可以声明多个可以用来代替__autoload函数作用的函数(类文件分布在不同的目录中可以使用这种方法)
<?php
function testFile(){
mkdir("C:/Users/Administrator/library");
$file = fopen("C:/Users/Administrator/library/A.class.php", ‘w‘);
fwrite($file, "<?php\n");
fwrite($file, "class A{}");
fclose($file);
}
spl_autoload_register(‘autoload1‘);
spl_autoload_register(‘autoload2‘);
function autoload1($className){
require "C:/Users/Administrator/library/{$className}.class.php";
}
function autoload2($className){
require "C:/Users/Administrator/class/{$className}.class.php";
}
testFile();
/*
1.先看这个文件中是否加载该类
2.autoload1运行后是否加载该类
3.autoload2运行后是否加载该类
*/
$obj = new A();
var_dump($obj);
浅克隆(默认):只能克隆对象中的“非对象非资源”数据
<?php
class C1{public $v1 = 1;}
class C2{
public $v2 = 2;
public $v3;
public function __construct(){
$this->v3 = new C1();
}
}
var_dump($o1 = new C2());
var_dump($o2 = clone $o1);//共用一个v3
深克隆:要想实现深克隆(一个对象的所有属性数据都彻底实现了“复制”),就需要对该对象类使用魔术方法__clone(),人为去复制浅克隆复制不了数据。
<?php
class C1{public $v1 = 1;}
class C2{
public $v2 = 2;
public $v3;
public function __construct(){
$this->v3 = new C1();
}
public function __clone(){
$this->v3 = clone $this->v3;
}
}
var_dump($o1 = new C2());
var_dump($o2 = clone $o1);//新建了个v3
http://www.jiaoanw.com/%E6%95%99%E6%A1%88%E6%A0%BC%E5%BC%8F/article-25814-23.html
http://www.jiaoanw.com/
true
教案网
http://www.jiaoanw.com/%E6%95%99%E6%A1%88%E6%A0%BC%E5%BC%8F/article-25814-23.html
report
1842
做法1:使用__autoload魔术函数 ?phpfunction testFile(){ mkdir("C:/Users/Administrator/library"); $file = fopen("C:/Users/Administrator/library/A.class.php", ‘w‘); fwrite($file, "?php\n");
从来不买