next Posted July 15, 2008 Share Posted July 15, 2008 I'm a bit confused with OOP, why even use regular methods like: $user = new User($a); $user->promote(); if the same can be accomplished in static method much easier: User::promote($a) I just feel like making all of my classes static. What am i missing in this picture? Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/ Share on other sites More sharing options...
KevinM1 Posted July 16, 2008 Share Posted July 16, 2008 I'm a bit confused with OOP, why even use regular methods like: $user = new User($a); $user->promote(); if the same can be accomplished in static method much easier: User::promote($a) I just feel like making all of my classes static. What am i missing in this picture? Static variables/methods are in the context of an entire class, not an individual object. They're commonly used with factories, where you want to get the result of a method without instantiating an object to do so. Somethng like: $myChar = CharGenerator::makeChar("mage"); You don't want to deal with a CharGenerator object itself. Rather, you just need the results from one of its methods. Your example of promoting a user doesn't really make much sense. Why? Because there's no User object that retains that promotion. Instead, the User class as a whole gets that promotion. That's why using a normal method is important: $Bob = new Employee("Robert", "Johnson"); $Bubba = new Employee("Bubba", "Smith"); $Bob->promote(5.25); $Bubba->promote(2.35); class Employee { private $firstName; private $lastName; private $hourlyWage = 10.00; public function __construct($firstName, $lastName) { $this->firstName = $firstName; $this->lastName = $lastName; } public function promote($newWage) { $this->hourlyWage += $newWage; } } Using a static method in this case wouldn't really make any sense, and wouldn't produce the results you wanted. Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-591890 Share on other sites More sharing options...
next Posted July 16, 2008 Author Share Posted July 16, 2008 Your example of promoting a user doesn't really make much sense. Why? Because there's no User object that retains that promotion. Huh? This doesn't make much sense to you: User::promote('Bob'); ? Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-592028 Share on other sites More sharing options...
KevinM1 Posted July 16, 2008 Share Posted July 16, 2008 Your example of promoting a user doesn't really make much sense. Why? Because there's no User object that retains that promotion. Huh? This doesn't make much sense to you: User::promote('Bob'); ? It depends on what you're trying to do. Right now, you're telling the class User to promote the string Bob. What does promote do? Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-592036 Share on other sites More sharing options...
next Posted July 16, 2008 Author Share Posted July 16, 2008 I tried to make up an example just now, i failed. I get what you mean now. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-592038 Share on other sites More sharing options...
KevinM1 Posted July 16, 2008 Share Posted July 16, 2008 I tried to make up an example just now, i failed. I get what you mean now. Thanks. Not a problem. I had the feeling I wasn't explaining it very well, so I was hoping you'd try doing it yourself. Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-592042 Share on other sites More sharing options...
cooldude832 Posted July 17, 2008 Share Posted July 17, 2008 Because the promotion is only good for the execution of that given php page. Setting a value such as that and not being able to recover it in any manner results in a process that yields no useful output. Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-592169 Share on other sites More sharing options...
next Posted July 17, 2008 Author Share Posted July 17, 2008 cooldude832, I just encountered that a minute ago Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-592828 Share on other sites More sharing options...
gizmola Posted July 19, 2008 Share Posted July 19, 2008 Stated even more simply, in your example you implied that the promote() method would act upon or send a message to a user. What $a was meant to be was unclear, but we could assume it might be a name, or array of information. The result of course was a "user" object. In your static example, you called promote, but on what? Well what happens is that an object is created, but immediately disposed of. You could think of it as: a) I create user "Bob". Then I promote() "Bob". whereas your static example is: b) PHP created an object and executed the static promote() method. So hopefully you can see that the two things you were comparing weren't the same. If your example had been: myclass::promote($a) vs. $user->promote($a) then it would be a more interesting question. Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-594078 Share on other sites More sharing options...
JonnoTheDev Posted August 7, 2008 Share Posted August 7, 2008 Also static methods are useful for generating objects of different types. Lets say we have an application that can connect to 3 different types of databases: sql, mysql, and oracle We dont want to have to write conditions in our calling code to determine what type of class to instantiate so we use a static method. The type of object returned could be based on the database connection string, so: // $db could be an instance of classes sql, mysql, or oracle $db = dbConnector::connect($connectionString); $db->query($query); The dbConnector class may look as follows: class dbConnector { public static function connect($string) { // some regex on the connection string $result = preg_match( switch($result) { case 1: return new sql($matches[2],$matches[1]); break; case 2: return new mysql($matches[1], $matches[2]); break; // etc.... } } } Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-610662 Share on other sites More sharing options...
deadonarrival Posted August 7, 2008 Share Posted August 7, 2008 Static works okay if you only want ONE of an object. But what if you want two different database connections? db::query(); won't be so useful when you want to have two seperate connections, but $main->query(); and $archive->query(); and $logs->query(); can be handy. Also, you don't always want people to be able to access your functions directly. Fair enough when it's your own code you can just not use them, but what when you want people to make their own modules for your CMS, for example? If they can just use auth::get_password(); they've got your users password... but if you make them use $auth->is_logged_in() and have the get_password() function private, along with the password variable, then the users can only check if they're logged in, not see the password. Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-610917 Share on other sites More sharing options...
448191 Posted August 7, 2008 Share Posted August 7, 2008 why use regular methods and not always static? Encapsulation/Static coupling That about sums it up. Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-610996 Share on other sites More sharing options...
deadonarrival Posted August 7, 2008 Share Posted August 7, 2008 448191 - it sums it up, assuming you know and understand the terms "Encapsulation" and "Static coupling" Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-611000 Share on other sites More sharing options...
DarkWater Posted August 7, 2008 Share Posted August 7, 2008 448191 - it sums it up, assuming you know and understand the terms "Encapsulation" and "Static coupling" If you're serious about writing an application mostly with objects, you should know those terms. If the thread starter doesn't...well, self-explanatory. =P If you don't know what they mean, just look them up. Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-611006 Share on other sites More sharing options...
448191 Posted August 7, 2008 Share Posted August 7, 2008 If a someone asks "what is fish?", do you show them a particular kind of fish, like a salmon, or whatever? Or, do you tell them "any of various cold-blooded, aquatic vertebrates, having gills, commonly fins, and typically an elongated body covered with scales" (dictionary.com), AND show them the salmon? Quote Link to comment https://forums.phpfreaks.com/topic/114894-why-use-regular-methods-and-not-always-static/#findComment-611016 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.