snivek Posted January 9, 2007 Share Posted January 9, 2007 If I create an instance of an object in index.php and within index.php include header.php. Can I have code in my header.php that uses the object as if it were in the file?index.php[code]<?php$site = new SiteCore;print $site->name;include(header.php)?>[/code]header.php[code]<title><?php print $site->name ?><title>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33496-using-a-class-in-an-included-file/ Share on other sites More sharing options...
taith Posted January 9, 2007 Share Posted January 9, 2007 yes, include/require basically just "crop" the included page's source, into the parent file... Quote Link to comment https://forums.phpfreaks.com/topic/33496-using-a-class-in-an-included-file/#findComment-156768 Share on other sites More sharing options...
Jessica Posted January 9, 2007 Share Posted January 9, 2007 Things like that are easy to try out. It should work, if you'd tried it you'd have seen ;) Quote Link to comment https://forums.phpfreaks.com/topic/33496-using-a-class-in-an-included-file/#findComment-156777 Share on other sites More sharing options...
snivek Posted January 9, 2007 Author Share Posted January 9, 2007 I did, and it didnt work. Which is why I posted :-\ Going to mess with it some more. Quote Link to comment https://forums.phpfreaks.com/topic/33496-using-a-class-in-an-included-file/#findComment-156783 Share on other sites More sharing options...
snivek Posted January 9, 2007 Author Share Posted January 9, 2007 I am getting "Call to a member function name() on a non-object in ..." when I call the method in the included file. Same exact method call from the base index.php file works fine. Quote Link to comment https://forums.phpfreaks.com/topic/33496-using-a-class-in-an-included-file/#findComment-156791 Share on other sites More sharing options...
trq Posted January 9, 2007 Share Posted January 9, 2007 Is the above example your exact code? It works as expected. Quote Link to comment https://forums.phpfreaks.com/topic/33496-using-a-class-in-an-included-file/#findComment-156838 Share on other sites More sharing options...
snivek Posted January 10, 2007 Author Share Posted January 10, 2007 Here is my code...the relevant parts anyway.SiteCore.php[code]<?phpClass SiteCore { private $_DOC_ROOT; private $_HEADER; private $_FOOTER; private $_IMAGES_DIR; private $_INCLUDE_DIR; private $_NAME; private $STYLESHEET; function __construct() { $this->_DOC_ROOT = $_SERVER['DOCUMENT_ROOT']; $this->_IMAGES_DIR = $this->_DOC_ROOT . "/images"; $this->_INCLUDE_DIR = $this->_DOC_ROOT . "/include"; $this->_NAME = "Family and Friends"; $this->_HEADER = $this->_INCLUDE_DIR . "/header.php"; $this->_FOOTER = $this->_INCLUDE_DIR . "/footer.php"; $this->_STYLESHEET = $this->_INCLUDE_DIR . "/stylesheet.css"; } public function print_header() { $header = $this->_HEADER; if (file_exists($header)) { require($header); } else { print "Header File Missing!"; } } public function name() { return $this->_NAME; }}?>[/code]index.php[code]<?phpRequire($_SERVER['DOCUMENT_ROOT'] . "/classes/SiteCore.php");$site = new SiteCore;$site->print_header();print $site->name();$site->print_footer();?>[/code]header.php[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/REC-html40/strict.dtd"><html><head><link rel="stylesheet" type="text/css"href="<? print $site->print_css();?>" /><title><?php print $site->name() ?></title> <!-- ** FAILING! ** --></head><body><div> <!-- GLOBAL CONTAINER --><?print $site->name();?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33496-using-a-class-in-an-included-file/#findComment-157292 Share on other sites More sharing options...
trq Posted January 10, 2007 Share Posted January 10, 2007 header.php isn't included in index.php anywhere, is there more code?This works without issue... you must not be including something correctly. Quote Link to comment https://forums.phpfreaks.com/topic/33496-using-a-class-in-an-included-file/#findComment-157295 Share on other sites More sharing options...
snivek Posted January 10, 2007 Author Share Posted January 10, 2007 It is included with SiteCore::print_header() method. It is called in index.php with $site->print_header(); Quote Link to comment https://forums.phpfreaks.com/topic/33496-using-a-class-in-an-included-file/#findComment-157297 Share on other sites More sharing options...
trq Posted January 10, 2007 Share Posted January 10, 2007 There is your problem then. $site is out of scope within that method, meens nothing. Try...[code=php:0]<?php print $this->name() ?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/33496-using-a-class-in-an-included-file/#findComment-157300 Share on other sites More sharing options...
snivek Posted January 10, 2007 Author Share Posted January 10, 2007 I think I see what you are saying. Since I included the header from within in the class method and not directly from index.php it cannot see $site? Like my header file is "running" inside of my SiteCore::print_header() method?I made the change and it worked! Thank you!Is what I'm doing a bad practice? Quote Link to comment https://forums.phpfreaks.com/topic/33496-using-a-class-in-an-included-file/#findComment-157316 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.