Unaccessible function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Unaccessible function

Post by orbstra »

Hey, I have a script with this type of architecture:

class Guava
->Function content()
-->Function content_method()

class Get
->function method()


the content_method() function is inside the content() function which is inside the Guava Class. I am sorry if this is confusing but ask me if you would like me to further explain the situation. Basically content_method() needs to be accessed by method() inside the Get Class, and because content_method() is inside a function inside a class, I am getting an error:
Fatal error: Call to undefined method Guava::content_method() in C:\Documents and Settings\Administrator\My Documents\My Projects\Web Projects\Guava\guav-includes\guav_library.php on line 229
Does anyone know how I can access the return value in content_method() from method()?

thanks, and sorry for the confusing explination
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

When a method defines a function inside of it, the function will be given global scope, not become a method of the class.
orbstra
Forum Commoner
Posts: 30
Joined: Thu Dec 07, 2006 5:07 pm

Post by orbstra »

How would I access the global scope in this case?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Like you would any other function.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

An example might help

Code: Select all

function globalFunctionABC() {
	echo 'abc ';
}

class Foo {
	function bar() {
		function globalFunctionXYZ() {
			echo 'xyz ';
		}
		echo 'bar ';
	}
}

$f = new Foo;
$f->bar();

globalFunctionABC();
globalFunctionXYZ(); // not a memeber of class Foo or an instance of Foo
// proof? Here you are
if ( method_exists($f, 'globalFunctionXYZ') ) {
	echo 'method exists ';
}
else {
	echo 'does not exist ';
}

$f->bar(); // <- Fatal error: Cannot redeclare globalfunctionxyz()
Post Reply