Zelphics Posted May 8, 2011 Share Posted May 8, 2011 heres an example of the code i have class someclass { public function run($parm) { system($parm); } public static function create($item1,$item2) { $this->run($item1 . $item2); } } then i have a file that attempts to use the create method: someclass::create($one,$two); when interpreting, i get this error: "Using $this when not in object context in" does anyone know how I can fix this? I think I understand why its wrong (the class hasn't really been set up) - I just dont know how to correct it. Is there a way I can access class functions without using $this ? additionally I tried making the run method static too, but that didnt seem to work. Quote Link to comment https://forums.phpfreaks.com/topic/235816-static-class-question/ Share on other sites More sharing options...
xyph Posted May 8, 2011 Share Posted May 8, 2011 You have to use self::run($item1 . $item2); Quote Link to comment https://forums.phpfreaks.com/topic/235816-static-class-question/#findComment-1212159 Share on other sites More sharing options...
MadTechie Posted May 8, 2011 Share Posted May 8, 2011 it will need to be static as well, or create the instance with static class someclass { public static function run($parm) { system($parm); } public static function create($item1,$item2) { self::run($item1 . $item2); } } someclass::create(1,2); new instance class someclass2 { public function run($parm) { system($parm); } public static function create($item1,$item2) { $c = new someclass2(); $c->run($item1 . $item2); } } someclass2::create(1,2); Quote Link to comment https://forums.phpfreaks.com/topic/235816-static-class-question/#findComment-1212161 Share on other sites More sharing options...
xyph Posted May 8, 2011 Share Posted May 8, 2011 It doesn't need to be static. In good practice, it should, but try this out: <?php class someclass { public function run($parm) { echo $parm; } public static function create($item1,$item2) { self::run($item1 . $item2); } } someclass::create('foo','bar'); ?> Quote Link to comment https://forums.phpfreaks.com/topic/235816-static-class-question/#findComment-1212167 Share on other sites More sharing options...
MadTechie Posted May 8, 2011 Share Posted May 8, 2011 What's this thing you call good practice Never heard of it.. lol Quote Link to comment https://forums.phpfreaks.com/topic/235816-static-class-question/#findComment-1212169 Share on other sites More sharing options...
xyph Posted May 8, 2011 Share Posted May 8, 2011 sadly, should != need. PHP is a little too loose in some cases. Allows bad coding habits. Quote Link to comment https://forums.phpfreaks.com/topic/235816-static-class-question/#findComment-1212173 Share on other sites More sharing options...
MadTechie Posted May 8, 2011 Share Posted May 8, 2011 I agree, for example PHP will use the name on an unset constance as the value here is a typical example, Hi All, I am no longer getting that error warning, I added the following to fix it error_reporting(0); <?php error_reporting(0); //Added echo $_POST[name]; when it should be $_POST['name'] Technically both will work but .. come on... (well now that's out of my system, i'll wait for a response from Zelphics, sorry for the temporary hi-jack ) Quote Link to comment https://forums.phpfreaks.com/topic/235816-static-class-question/#findComment-1212179 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.