Jump to content


php oop error i cant figure out


3 replies to this topic

#1 trs21219

    Young Padawan

  • Members
  • Pip
  • 31 posts
  • Gender:Male
  • Location:Pittsburgh, Pa

Posted 16 September 2007 - 09:14 PM

ok ive been trying to figure this out for a while now...ive googled it and looked on oop tutorials and cant figure out whats wrong.

my index file is as follows:
<?php
require_once('include/System_class.php');
?>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php $System -> Layout -> setCSS('style_main','v1');?>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title><?php $System -> Configuration -> getSiteTitle();?></title>
</head>
	<body>
		<div align='center'>
			<div class="wrapper">
				<div class="container">
					  <div class="header">
					<?php $System -> Layout -> getHeader();?>
				   
 </div>
					<div class="content">
					<?php $System -> Layout -> getContent();?>
					</div>
				 	<div class="footer">
					<?php $System -> Layout -> getFooter();?>
					</div>
				</div>
			</div>
		</div>
	</body>
</html>

my System_class.php
<?php
include('include/classes/class_configuration.php');
$System -> Config = new Configuration;

include('include/classes/class_database.php');
$System -> DB = new Database;

include('include/classes/class_games.php');
$System -> Games = new Games;

include('include/classes/class_layout.php');
$System -> Layout = new Layout;

include('include/classes/class_media.php');
$System -> Media = new Media;

include('include/classes/class_security.php');
$System -> Security = new Security;

include('include/classes/class_users.php');
$System -> Users = new Users;

?>

layout class
<?php

class Layout
{
	// Including Pages for Layout
	function getHeader()
	{
		include('include/layout/header.php');
	}
	function getContent()
	{
		$this -> getMenu();
		$this -> getMemberBar();
		$this -> getContentBody();
	}
	function getFooter()
	{
		include('include/layout/footer.php');
	}
	function getMenu()
	{
		echo "<div class='menu'>";
		include('include/layout/menu.php');
		echo "</div>";
	}
	function getContentBody()
	{
		echo "<div class='contentbody'>";
		include('include/layout/content.php');
		echo "</div>";
	}
	function getMemberBar()
	{
		echo "<div class='memberbar'>";
		echo "memberbar is here";
		echo "</div>";
	}
	//end
	
	//Set the css for each page
	function setCSS($name, $version)
	{
		echo "<link href=\"http://" . $_SERVER['HTTP_HOST'] . "/layout/" . $version . "/" . $name . ".css\" rel=\"stylesheet\" type=\"text/css\">";
	}


}
?>

config class
<?php

class Configuration
{
	function getSiteTitle()
	{
		if(isset($pagename))
		{
			echo $pagename . " - AlliedDefiance.com";
		}
		else
		{
			echo "AlliedDefiance.com";
		}
	}
	function showSiteName()
	{
		echo "Allied Defiance";
	}
}
?>

and my footer.php
<p>

All Content &copy; 2007 <a href="http://www.allieddefiance.com"><?php $System -> Config -> showSiteName();?></a><br />

</p>

now the problem is that all the system functions work in the index.php file but when i call them from other files i get the Fatal error: Call to a member function on a non-object error. the files are included so they should inherit the class thats set in the index shouldnt they? could someone please tell me whats happening here and how to fix it? im new at OOP but i know php.

Edited by trs21219, 16 September 2007 - 09:59 PM.


#2 rc69

    PHP Master PD

  • P2L Staff
  • PipPipPipPip
  • 3,827 posts
  • Gender:Male
  • Location:Here
  • Interests:Web Development

Posted 17 September 2007 - 04:54 PM

If you want urgent help, a forum is not the place to go, especially at night. 24-hours is the basic minimum before you should consider double posting. Less then that and you're topic will still be top 5, which is more than visible to everybody.

Could you possibly post the full error that you get, file/line numbers included?

If you know php, then this is a simple problem of the $system variable not making it to where you want it to (possible globals problem). OOP doesn't change that (much).

Edited by rc69, 17 September 2007 - 04:55 PM.


#3 Demonslay

    P2L Jedi

  • Members
  • PipPipPip
  • 970 posts
  • Gender:Male
  • Location:A strange world where water falls out of the sky... for no reason.
  • Interests:Graphic Design, Coding, Splinter Cell, Cats

Posted 17 September 2007 - 04:59 PM

Where is $System defined?

I find your methods quite interesting really. I usually see them all under different global variables, rather than one class that has all of the others instanced into it. I can actually see this being cleaner...

Anyways, when you have a class file that requires another class to work correctly, such as for inheritance, you should always check that the file exists (unless you are using PHP5, which you could simply define an __autoload() function).

Where are you receiving the errors, and on what method calls? This is important if we are to tell when and why the problem is occurring.

#4 trs21219

    Young Padawan

  • Members
  • Pip
  • 31 posts
  • Gender:Male
  • Location:Pittsburgh, Pa

Posted 17 September 2007 - 09:00 PM

oops i found my error in it... i was including the pages within the class and that was making them part of the layout class instead of them using the layout class.

thanks for your help guys and i am sorry for double posting before RC





1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users