Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

calling class method within required/included script

Status
Not open for further replies.

southbeach

Programmer
Jan 22, 2008
879
0
0
US
OK, I have three PHP scripts: index.php, common/common.php and classes/main.php

index.php is, of course, my default script/page. Here, I use
Code:
require("classes/main.php");
require("common/common.php");

$users = new users();

I also have routines where I conditionally call my class methods within index.php - These work very nicely!

The problem rises when I try to use my class methods within common/common.php. I call a function which in turn will call on class methods, same as index.php, but I get errors

Code:
Fatal error: Call to undefined method stdClass::listusers()

I am not using "public" or anything too fancy in my class methods (newbie) so, as I hit the web looking for answer, I figure I drop this question in hoping for your usual assistance.

Thanks!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Well, I decided to simply do

$users = new users();

within my function in common/common.php and I now can call my class methods. I had not done this to begin with thinking I would end-up with some sort of error for instanciating the same class multiple times.



--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
the standard class (stdclass) has only the build in magic methods. if your class have been called Users and had that class had a (public) method of listUsers, there is no reason why the call to users::listUsers() would not work. I use something similar all the time although I abstract it through a factory class.

 
I've done some reading and I recall reading that ALL classes are "PUBLIC" by default so I do not "implicitly" use the public declaration.

I will give this a try and see if it makes a difference - Will report back later!


--
SouthBeach
The good thing about not knowing is the opportunity to learn - Yours truly, 2008.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top