Jump to content

am i calling thee the right way?


blueman378

Recommended Posts

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...

Link to comment
https://forums.phpfreaks.com/topic/164034-am-i-calling-thee-the-right-way/
Share on other sites

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.

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

@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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.