•   欢迎来到21NN网.
  •   请记住本站网址www.21nn.cn

php挪用差别目次的类【php问题】,php,调用类

摘要: php挪用差别目次的类1、起首php把差别目次的类的相对途径存入数组,然后运用轮回引入类<?phpheader('Content-type:text/html;charset=u...

php挪用差别目次的类

1、起首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);
}
}
});

2、运用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相干学问,请接见ki4网!

以上就是php挪用差别目次的类的细致内容,更多请关注ki4网别的相干文章!

分享到:

发表评论

评论列表

还没有评论,快来说点什么吧~

公众号二维码

微信公众号