Leppy Posted February 3, 2009 Share Posted February 3, 2009 Hello, I've been doing some research on this forum and I haven't really found anything that answer my question which is, what is the difference between static and non-static methods and variables? The main reason why I am asking is because I like to use the syntax class::method() or class::$variable because it makes more sense and also because when I use the class within function I do not have to declare GLOBAL $my_class. Another reason why I'm asking is, in order to be able to use the methods or variable I call using class::method() or class::$variable, I need to specify the "static" keyword all the time otherwise I get a PHP error #2048 Non-static method which I find very annoying. That being said, what is the difference between static and non-static? When should "static" be used and why? Any help is appreciated, Thank you. Leppy- Link to comment https://forums.phpfreaks.com/topic/143587-solved-php5-static-vs-non-static/ Share on other sites More sharing options...
waynew Posted February 3, 2009 Share Posted February 3, 2009 Static variables and methods can be accessed without actually having to instantiate the class. Static method MyClassName::static_method(); instead of $obj = new MyClassName(); $obj->method(); Link to comment https://forums.phpfreaks.com/topic/143587-solved-php5-static-vs-non-static/#findComment-753416 Share on other sites More sharing options...
printf Posted February 3, 2009 Share Posted February 3, 2009 class static variables, (module static variables) store information pertaining to class instances. As for methods, the difference between a method being defined as static or not is simply this. static = method is a member of the given class. non-static = method is a member of a instance of the given class. For a simple example of usage... Say you have web crawler class that handles fetching documents in parallel. In that class you may have variables (rules, cookies, domain caches) that need to be shared between all instances of that class, in order to keep your application from doing things that may have already been done. So you declare the variable as static because that variable is common to all instances. Think of it, an instance can see it's class, a class can never see it's instance, so a declared static variable or method gives all instances of a class a way to share common variables, (information) declared static between it's instances. Link to comment https://forums.phpfreaks.com/topic/143587-solved-php5-static-vs-non-static/#findComment-753421 Share on other sites More sharing options...
Leppy Posted February 3, 2009 Author Share Posted February 3, 2009 Static variables and methods can be accessed without actually having to instantiate the class. Static method MyClassName::static_method(); instead of $obj = new MyClassName(); $obj->method(); I see, I didn't know that. Thanks! Link to comment https://forums.phpfreaks.com/topic/143587-solved-php5-static-vs-non-static/#findComment-753423 Share on other sites More sharing options...
Leppy Posted February 3, 2009 Author Share Posted February 3, 2009 class static variables, (module static variables) store information pertaining to class instances. As for methods, the difference between a method being defined as static or not is simply this. static = method is a member of the given class. non-static = method is a member of a instance of the given class. For a simple example of usage... Say you have web crawler class that handles fetching documents in parallel. In that class you may have variables (rules, cookies, domain caches) that need to be shared between all instances of that class, in order to keep your application from doing things that may have already been done. So you declare the variable as static because that variable is common to all instances. Think of it, an instance can see it's class, a class can never see it's instance, so a declared static variable or method gives all instances of a class a way to share common variables, (information) declared static between it's instances. Well said, I understand now. Thank you! Link to comment https://forums.phpfreaks.com/topic/143587-solved-php5-static-vs-non-static/#findComment-753424 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.