Home  >  Article  >  Backend Development  >  PHP对象问题

PHP对象问题

WBOY
WBOYOriginal
2016-06-23 13:54:351198browse

我写了一个类,比如 class MyClass,
然后我在其中一个页面比如a.php,我实例化 $obj1=new MyClass();
那我的这个已经实例化的对象$obj1能被其它页面引用吗?肯定是不能的。那如果我想在b.php页面里引用,我还需要另外实例化吗?那是不是很麻烦....


回复讨论(解决方案)

class MyClass  把这个类的方法写成静态方法,这样在每个页面都能直接使用了
 如:MyClass::func();....

还需要包含一下这个类文件

<?php#function.phpclass test{	private static $_instance = NULL;	public function a(){	    echo 'metchod a';	}	public function b(){	    echo 'method b';	}    public static function instance(){        if(self::$_instance === NULL){            self::$_instance  = new self();        }        return self::$_instance;    }}#a.php//include function.phpecho test::instance()->a();#b.php//include function.phpecho test::instance()->b();

忘了静态类这个东东了。谢谢!

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