php调用不同目录的类-PHP问题

资源魔 32 0

php挪用没有同目次的类

一、起首php把没有同目次的类的绝对门路存入数组,而后应用轮回引入类

<?php 
header('Content-type:text/html;charset=utf-8'); 
//spl_autoload_register()参数是匿名函数
spl_autoload_register(function($ClassName){
//将没有同门路的类文件的门路放入数组中;
$arr = array(
"./$ClassName.class.php",
"./admin/controller/$ClassName.class.php"
);
// 轮回没有同门路的类文件的数组
foreach ($arr as $filename) {
if (file_exists($filename)) {
require_once($filename);
}
}
});

二、应用new要害字创立工具并挪用便可

$obj = new Student();
$obj->ShowInfo();
$obj2 = new Fruit();
$obj2->ShowInfo();

类文件:定名为Student.class.php

<?php
header('Content-type:text/html;charset=utf-8'); 
final class Student{
const TILTLE = "3班";
private $name = "李立";
private $age = 20;
public function __construct(){
echo "{$this->name}的春秋是{$this->age}<br>";
}
public function ShowInfo(){
echo "{$this->name}的班级是".self::TILTLE."<br>";
}
}

类文件:定名为Fruit.class.php

<?php
header('Content-type:text/html;charset=utf-8'); 
final class Fruit{
const TILTLE = "生果类";
private $name = "苹果";
private $price = '20元/kg';
public function __construct(){
echo "{$this->name}的价钱是{$this->price}<br>";
}
public function ShowInfo(){
echo "{$this->name}属于".self::TILTLE."<br>";
}
}

更多PHP相干常识,请拜访PHP中文网!

以上就是php挪用没有同目次的类的具体内容,更多请存眷资源魔其它相干文章!

标签: php php教程 php故障解决 php使用问题 调用类

抱歉,评论功能暂时关闭!