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 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分