Heim  >  Artikel  >  Backend-Entwicklung  >  PHP对象问题

PHP对象问题

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

我写了一个类,比如 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();

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

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:是否可以利用REST API上传文件?Nächster Artikel:关于URL解码