0%

Php 可以动态的 new 一个变量类名

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
header("content-type:text/html; charset=utf-8");
//echo ucfirst('a b');class Student{
private $xh;
private $name;function __construct($id,$name){
$this->id = $id;
$this->name = $name;
}
function hello(){
echo '我的学号是:'.$this->id.'我的名字是:'.$this->name;
}
}
$className = 'Student';
$stu = new $className('123456','abc');
$stu->hello();
?>

页面打印结果如下:我的学号是:123456我的名字是:abc

欢迎关注我的其它发布渠道