dongshuan8722 2012-08-21 13:00
浏览 33
已采纳

包含在内的全局对象

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...

  • 写回答

1条回答 默认 最新

  • doumanni3501 2012-08-21 13:06
    关注

    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?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探