blueman378 Posted June 29, 2009 Share Posted June 29, 2009 Hi guys, Well heres the error: Fatal error: Call to a member function build_url() on a non-object in E:\xampp\htdocs\test\extends\users.php on line 27 Yet it should work. Heres the code: <?php /** * FILENAME: index.php * DESCRIPTION: The main page users will see * DATE: June 2009 */ include("extends/users.php"); ?> <html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <head> <title><?php echo $settings->get_setting(sitename); ?></title> </head> <body> <?php echo $settings->build_url(); ?> </body> </html> <?php /** * FILENAME: constants.php * DESCRIPTION: holds user defined variables * DATE: June 2009 */ class constants { private $sitename = 'NZ EVOX\'s Login Script'; private $site_root = 'localhost/test/'; private $site_prefix = 'http://'; private $nondefined = "NOT DEFINED"; //-------------------------------------------------------------------- // FUNCTION: __construct() // INPUTS: // RETURN: //-------------------------------------------------------------------- function __construct() { } //-------------------------------------------------------------------- // FUNCTION: get_setting( $setting ) // INPUTS: $setting: name of setting that you wish to read(MUST EXIST) // RETURN: value of setting or null defined //-------------------------------------------------------------------- public function get_setting( $setting ) { if(isset($this->$setting)) return $this->$setting; else return $nondefined; } //-------------------------------------------------------------------- //-------------------------------------------------------------------- // FUNCTION: set_setting( $setting, $value ) // INPUTS: $setting: name of setting that you wish to set(MUST EXIST) // RETURN: either nothing or null defined //-------------------------------------------------------------------- public function set_setting( $setting, $value = "" ) { if(isset($this->$setting)) $this->$setting = $value; else return $nondefined; } //-------------------------------------------------------------------- //-------------------------------------------------------------------- // FUNCTION: build_url( $page = "" ) // INPUTS: $setting: name of setting that you wish to set(MUST EXIST) // RETURN: either nothing or null defined //-------------------------------------------------------------------- public function build_url( $page = "" ) { $url = $this->site_prefix; $url .= $this->site_root; $url .= $page; return $url; } //-------------------------------------------------------------------- } $settings = new constants; ?> <?php /** * FILENAME: users.php * DESCRIPTION: holds the class for managing the users * DATE: June 2009 */ include("constants.php"); class userm { private $userid; private $userlevel; private $from; private $nondefined = "NOT DEFINED"; //-------------------------------------------------------------------- // FUNCTION: __construct() // INPUTS: // RETURN: //-------------------------------------------------------------------- function __construct() { if(isset($_REQUEST['from'])) $this->from = $_SESSION['from'] = $_REQUEST['from']; elseif(isset($_SESSION['from'])) $this->from = $_SESSION['from']; else $this->from = $settings->build_url(); } } $user = new userm; ?> im obviously missing something simple... just dont know what... Quote Link to comment https://forums.phpfreaks.com/topic/164034-am-i-calling-thee-the-right-way/ Share on other sites More sharing options...
Ken2k7 Posted June 29, 2009 Share Posted June 29, 2009 1. Is sitename a constant? I don't see it defined anywhere. It should probably have quotes around it like so - <?php echo $settings->get_setting('sitename'); ?> 2. In your get_setting and set_setting methods, you have a return $nondefined, where $nondefined is not defined. I think you meant $this->nondefined. Quote Link to comment https://forums.phpfreaks.com/topic/164034-am-i-calling-thee-the-right-way/#findComment-865357 Share on other sites More sharing options...
Philip Posted June 29, 2009 Share Posted June 29, 2009 Actually, for the non-object problem: $settings->build_url(); $settings isn't defined anywhere - thus you can't call a method on a non-object. Quote Link to comment https://forums.phpfreaks.com/topic/164034-am-i-calling-thee-the-right-way/#findComment-865358 Share on other sites More sharing options...
Ken2k7 Posted June 29, 2009 Share Posted June 29, 2009 Actually, for the non-object problem: $settings->build_url(); $settings isn't defined anywhere - thus you can't call a method on a non-object. I think it is. The first include loads the userm file which loads constants and $settings is defined in there. Quote Link to comment https://forums.phpfreaks.com/topic/164034-am-i-calling-thee-the-right-way/#findComment-865360 Share on other sites More sharing options...
Philip Posted June 29, 2009 Share Posted June 29, 2009 It is all about the scope though. Example script you can test with: <?php error_reporting(E_ALL); $var = 'test'; class scope { public $test = 'scoped'; function __construct( ) { echo $var; echo '<br>'; echo $this->test; } } $myClass = new scope; ?> Notice: Undefined variable: var in /home/././...php on line 9 scoped Quote Link to comment https://forums.phpfreaks.com/topic/164034-am-i-calling-thee-the-right-way/#findComment-865363 Share on other sites More sharing options...
Ken2k7 Posted June 29, 2009 Share Posted June 29, 2009 KingPhilip, are you speaking of the $settings variable used in the userm class? Oh one more thing, it's not $this->$settings but $this->settings. Quote Link to comment https://forums.phpfreaks.com/topic/164034-am-i-calling-thee-the-right-way/#findComment-865366 Share on other sites More sharing options...
Philip Posted June 29, 2009 Share Posted June 29, 2009 @Ken - yes. It is even stated that is a non-object in the error: Fatal error: Call to a member function build_url() on a non-object in E:\xampp\htdocs\test\extends\users.php on line 27 /** * FILENAME: users.php * DESCRIPTION: holds the class for managing the users * DATE: June 2009 */ The only call to a method of build_url is using the $settings variable. Quote Link to comment https://forums.phpfreaks.com/topic/164034-am-i-calling-thee-the-right-way/#findComment-865369 Share on other sites More sharing options...
blueman378 Posted June 29, 2009 Author Share Posted June 29, 2009 Oh one more thing, it's not $this->$settings but $this->settings. to where are you referring? Quote Link to comment https://forums.phpfreaks.com/topic/164034-am-i-calling-thee-the-right-way/#findComment-865375 Share on other sites More sharing options...
blueman378 Posted June 29, 2009 Author Share Posted June 29, 2009 btw i get what you mean about the only instance of the class being $settings but how would i pass that into the scope of userm, i mean i dont want to use global lol one option would be to make userm extend constant i guess but i really dont want to. Quote Link to comment https://forums.phpfreaks.com/topic/164034-am-i-calling-thee-the-right-way/#findComment-865382 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.