包含在内的全局对象

包含在内的全局对象

问题描述:

Righty-o

I am fairly new to OOP.

Im using PHP v5+

I have my index.php:

<?
include_once('class/includes.php');
$reg = new register;
$action = $_GET['action'];
$page = $_GET['page'];
include('header.php');
if($page):
include($page);
else:
include('home.php');
endif;
include('footer.php');
?>

i have my htaccess set up so that the url for each page is foo.com/pagename

Thats fine.

on my classes.php page (foo.com/classes) which as default includes viewClasses.php //note classes refers to yoga classes rather than OOP classes

so essentially i have 2 levels of includes (index.php including classes.php which includes viewClass.php)

this is my viewClass.php

<?
global $reg;
$reg->viewClassList();
echo $reg->classList;

?>

For some reason it is not picking up $reg as defined in index.php and therefore is giving me this error:

Fatal error: Call to a member function viewClassList() on a non-object in /home/pandazco/public_html/register/viewClass.php on line 3

I hope this explains my problem well enough...

Righty-o p>

我对OOP还不熟悉。 p>

我正在使用PHP v5 + p>

我有我的index.php: p>

 &lt;?  
include_once('class / includes.php'); 
 $ reg = new register; 
 $ action = $ _GET ['action']; 
 $ page = $ _GET ['page']; 
include('  header.php文件 '); 
如果($页):
include($页); 
else:
include(' home.php '); 
endif; 
include(' footer.php'); 
&GT?  ; 
  code>  pre> 
 
 

我设置了htaccess,以便每个页面的网址都是foo.com/pagename

nn

这是好的 。 p>

在我的classes.php页面(foo.com/classes)上,默认情况下包括viewClasses.php // note类引用瑜伽类而不是OOP类 p>

基本上我有2个包含级别(index.php包括classes.php,包括viewClass.php) p>

这是我的viewClass.php p>

 &lt;?
global $ reg; 
 $ reg-&gt; viewClassList(); 
echo $ reg-&gt; classList; 
 
?&gt; 
  code  >  pre> 
 
 

出于某种原因,它没有按照index.php中的定义获取$ reg,因此给出了这个错误: p>

致命错误: 打电话给 第3行/home/pandazco/public_html/register/viewClass.php中非对象的成员函数viewClassList() p>

我希望这足以解释我的问题... p> div>

The problem appears to be that you are not including the instantiation in your viewClass.php which means that $reg is nothing. This is what the error is telling you.

However I would never use global vars, in fact I am pretty sure there are many lengthy discussions on not using them. Instead I would set a session variable and read from this instead if you need to pass data between different php scripts.

Additionally why are you using $reg as a global anyway? All you appear to do is instantiate it in index.php and try to call it in viewClass.php. What are you trying to achieve exactly?