TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 09-17-2007, 04:01 AM   #1 (permalink)
The Wanderer
 
Join Date: Sep 2007
Posts: 12
Thanks: 0
trs21219 is on a distinguished road
OOP Problem

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:
Code:
<?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
Code:
<?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
Code:
<?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
Code:
<?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
Code:
<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.
trs21219 is offline  
Reply With Quote
Old 09-17-2007, 01:15 PM   #2 (permalink)
The Reckoner
Advanced Programmer Top Contributor 
 
Karl's Avatar
 
Join Date: Sep 2007
Posts: 437
Thanks: 22
Karl is on a distinguished road
Default

Well that error implies that you are trying to call a function of an object that is not a valid object. For instance

$p->my_function();

instead of

$p = new MyObject();
$p->my_function();

At a guess, I would say that the object is being instantiated in index.php (or a file included by index.php) and so therefore that file is working fine. However, in the other files (where you get an error), the error would suggest that you are not initialising an instance of the class, but rather, trying to access a function of a non existent class. I would ensure that the class is instantiated by looking back at index.php and it's included files, then ensuring you do the same on subsequent pages.
Karl is offline  
Reply With Quote
Old 09-17-2007, 01:24 PM   #3 (permalink)
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I see the problem here. As you are including the files in your class itself, you're correct in that it inherits it, but it retains its scope. Therefore, take header.php as our example, because it is being included in the getHeader() function it will be included correctly, but be classed as part of the class.

Thus, in header.php you can still call the functions in your Layout class, but instead of using the object itself that you created when initiating your class, in your example, $System->Layout, it would simply by $this.

Wrong:

PHP Code:
<?php echo $System->Config->showSiteName();?>
Right:

PHP Code:
<?php echo $this->showSiteName();?>
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote
Old 09-17-2007, 08:27 PM   #4 (permalink)
The Wanderer
 
Join Date: Sep 2007
Posts: 12
Thanks: 0
trs21219 is on a distinguished road
Default

so if i just normally include the files in the index.php instead of the class should the class then work?
trs21219 is offline  
Reply With Quote
Old 09-17-2007, 08:32 PM   #5 (permalink)
The Wanderer
 
Join Date: Sep 2007
Posts: 12
Thanks: 0
trs21219 is on a distinguished road
Default

ahh yes it does work if i do that... thank you so much for your help wildhoney. i knew it would be something small and stupid but didnt know what it could be.
trs21219 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 02:24 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design