...

View Full Version : Resolved Access Object methods within seperate function



yeshuawatso
03-27-2009, 08:07 AM
Hello fellow coders it's great to be among the best.

I'm having a little trouble understanding how to access object methods within a new function.

Example:


//I have initiated a new object:
$objclass = new class();

//Ran a method:
$objclass->someFunction;

//But now, I want to access this same object and run a few more methods but within a function:

function test($var, $var2, $var3=0){
$objclass->someOtherFunction($var, $var2);
}

//Continue to access the same object.
$objclass->anotherFunction;

Whenever I try this I get this error:

Call to a member function someOtherFunction() on a non-object.

Is what I'm trying to do possible or have I missed something? I have tried passing the entire object into the function but this just spits out errors about how the object is being accessed incorrectly.

I've done some searches on this but the results seem to apply to Java or C#/C++.

Any help would be appreciated.

Thanks,
Yeshua

Resolved

oesxyl
03-27-2009, 08:12 AM
$objclass scope is local to your function, test. Two way to do that:
- declare $objclass global inside test function
- pass $objclass as argument to function test.( I prefere that)

best regards

abduraooft
03-27-2009, 08:12 AM
function test($var, $var2, $var3=0){
$objclass->someOtherFunction($var, $var2);
}
Your $objclass has no scope inside that function. So you'd need to pass this object as the fourth parameter to your function.

yeshuawatso
03-27-2009, 08:25 AM
Your $objclass has no scope inside that function. So you'd need to pass this object as the fourth parameter to your function.

This is what I feared I would have to do. Oh well, I can always grit my teeth and debug.:(

yeshuawatso
03-27-2009, 08:31 AM
Would passing the object have the same effect if sets and gets are being accessed?

Example

//I have initiated a new object:
$objclass = new class();

//Ran a method:
$objclass->someFunction->setSomething;

//But now, I want to access this same object and run a few more methods but within a function:

function test($var, $var2, $var3=0,$objclass){
$objclass->someFunction->setSomethingElse($var, $var2);
}

//Continue to access the same object.
$objclass->someFunction->getSomething;

Thanks
Yeshua

yeshuawatso
03-27-2009, 08:37 AM
Nevermind, I got it to work as suggested.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum