Home > Backend Development > PHP Tutorial > 将一个类赋值的问题

将一个类赋值的问题

WBOY
Release: 2016-06-23 14:03:48
Original
1246 people have browsed it

定义了一个类

class myclass{   function test()      echo "good";}$test1="myclass";$test2=new myclass();
Copy after login


都是把类赋值给变量,那这两种赋值方法有什么差别?


回复讨论(解决方案)

你分??出那???量就知道啦

$test1只是字符串
$test2是类的实例,是个对象
对两者gettype()可以看出区别

<?phpclass MyClassq{    const constant = 'constant value';    function showConstant() {        echo  self::constant . "\n";    }}echo MyClassq::constant . "\n";$classname = "MyClassq";echo $classname::constant . "\n"; // PHP 5.3.0之后$class = new MyClassq();$class->showConstant();echo $class::constant."\n"; // PHP 5.3.0之后?> 
Copy after login

上面说的$test1 的确是字符串,echo $classname::constant . "\n";为什么能访问到类里面的内容?

复习一下“变量的变量”

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template