Jump to content

Recommended Posts

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?

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.

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?

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.

 

  • 3 weeks later...

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....
      }
   }
}

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.

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.

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?

 

;)

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.