Home > Backend Development > PHP Tutorial > 关于php全局自定义变量的求解!

关于php全局自定义变量的求解!

WBOY
Release: 2016-06-23 14:05:14
Original
976 people have browsed it

概念还是不清,用不明白,笨人呀!!!
我想写一个整个站的全局变量,在哪个类中都可共享的
就比如我在a类中初始化该全局变量,然后就可以在其它类中读写共享该变量,求哪位老大写个范例,感激!!!


回复讨论(解决方案)

用session或者cookie

那你直接写个基类,声明一个变量呀...

那你直接写个基类,声明一个变量呀...

求范例,感激!!!

在类的方法中要使用得用global 引用一下
比如

$a="123";class dd{   function show(){     global $a;     $a="cc";   }}class cc{   function show(){     global $a;     return $a;   }}$d= new dd();$d-> show();$c= new cc();echo $c-> show();
Copy after login

可以看到最后输出的结果是cc

<?php//父类class BaseClass {	public static $name = "";	public function __construct(){	}}//子类一class son1Class extends BaseClass{	public function initName($name){		parent::$name = $name;	}}//子类二class son2Class extends BaseClass{	public function index(){		return parent::$name;	}}//实例化类一$a = new son1Class();//初始化$name$a->initName("PHPer");//实例化类二$b = new son2Class();//获取$name的值$name = $b->index();//在son1Class初始化一$name值,在son2Class中也是可以调用的...echo $name; //输出PHPer
Copy after login

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