Anti-Moronic Posted January 17, 2010 Share Posted January 17, 2010 I *think* this has something to do with the way I've started using classes. I used to do this: $class = classname(); $class->classfunction(); but now I simply do this: classname::classfunction Yet now when I use $this within 'classname' it says: "Using $this when not in object context" If that is the problem, is there any other way to use 'this' with this method or will I have to create the object first? Appreciate any help. Quote Link to comment https://forums.phpfreaks.com/topic/188775-using-this-but-not-working/ Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 Using :: is used for static functions.. You can only use $this in the case of your first example.. Quote Link to comment https://forums.phpfreaks.com/topic/188775-using-this-but-not-working/#findComment-996636 Share on other sites More sharing options...
phonydream Posted January 17, 2010 Share Posted January 17, 2010 You'll have to create the object first. Unless you are within the class itself, in which case you can use class::classfunction Quote Link to comment https://forums.phpfreaks.com/topic/188775-using-this-but-not-working/#findComment-996638 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 Should have read the whole question If you dont want to create the object you can use self::function_name(); inside the class instead of $this Quote Link to comment https://forums.phpfreaks.com/topic/188775-using-this-but-not-working/#findComment-996640 Share on other sites More sharing options...
Anti-Moronic Posted January 17, 2010 Author Share Posted January 17, 2010 ..thanks! I tend to use classname::classfunction(); quite a lot now when outside classes instead of creating an object at the top of my scripts. Does this have any performance impact? classname::classfunction1(); ..... classname::classfunction2(); OR $classname = classname(); $classname->classfunction1(); .... $classname->classfunction2(); Thanks again, appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/188775-using-this-but-not-working/#findComment-996668 Share on other sites More sharing options...
Buddski Posted January 17, 2010 Share Posted January 17, 2010 I dont know, exactly, but if i was to have a guess.. classname::classfunction(), i would assume uses less memory because you dont have to create an object to access its function.. I could be wrong Quote Link to comment https://forums.phpfreaks.com/topic/188775-using-this-but-not-working/#findComment-996670 Share on other sites More sharing options...
Daniel0 Posted January 17, 2010 Share Posted January 17, 2010 Calling static methods is faster than using objects. If all you're doing is calling static methods on objects (of if you do it interchangeably) there is no point in using classes in the first place. Quote Link to comment https://forums.phpfreaks.com/topic/188775-using-this-but-not-working/#findComment-996680 Share on other sites More sharing options...
Anti-Moronic Posted January 17, 2010 Author Share Posted January 17, 2010 Thanks Daniel! ..and good point. I don't think I've come up against anything which requires me to use classes in such a way and I must admit, I am a bit out of my depth when it comes to OOP. I guess I use classes to arrange my functions then Quote Link to comment https://forums.phpfreaks.com/topic/188775-using-this-but-not-working/#findComment-996684 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.