Home  >  Article  >  php教程  >  全局对象_php教程

全局对象_php教程

PHP中文网
PHP中文网Original
2016-05-23 08:39:031445browse

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();
Statement:
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