Home > php教程 > PHP源码 > body text

全局对象_php教程

PHP中文网
Release: 2016-05-23 08:39:03
Original
1450 people have browsed it

php代码

<?php
function A($class){
	if(isset($GLOBALS[$class])){
		if(is_object($GLOBALS[$class])){
			return $GLOBALS[$class];
		}
	}
	return $GLOBALS[$class] = new $class();
}

class MyClass{
	public function getStr(){
		return "hh";
	}
	public function getStr1(){
		return "cc";
	}
	public function getJson(){
		return json_encode(array("name"=>"你好"));
	}
	public function getAll(){
		$str1 = A("MyClass")->getStr();
		$str2 = A("MyClass")->getStr1();
		$json = A("MyClass")->getJson();
		return $str1.$str2." ".$json;
	}
}
echo A("MyClass")->getAll();
Copy after login
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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template