Jump to content

Advancing My OOP Skills :)


seventheyejosh

Recommended Posts

So I've been trying to furthor my OOP knowledge, so I've been trawling through the php.net manual as well as countless online tutorials. I've finally clicked about visibility, but I have a question about extends.

 

What is a real world example when you would do class a extends class b?

 

And has anyone ever used the final decleration?

 

Thanks a lot :)

 

Link to comment
Share on other sites

So I've been trying to furthor my OOP knowledge, so I've been trawling through the php.net manual as well as countless online tutorials. I've finally clicked about visibility, but I have a question about extends.

 

What is a real world example when you would do class a extends class b?

 

And has anyone ever used the final decleration?

 

Thanks a lot :)

 

 

Extending a class is used often in the Command pattern.

 

abstract class Command
{
   private $request;

   public function __construct(Request $request)
   {
      $this->request = $request;
   }

   public abstract function execute();
}

class Login_Command extends Command
{
   public function execute()
   {
      //assume we already have a db connection
      //request validation goes here...if valid $user = $this->request->getValue('user') and $pass = md5($this->request->getValue('pass'))

      $result = mysql_query("SELECT * FROM users WHERE username = $user AND pass = $pass");

      if (mysql_num_rows($result) == 1) { /* success */ }
      else { /* error */ }
   }
}

 

Rough sketch, but you should get the idea.

Link to comment
Share on other sites

final is really not used all that often.

 

 

The only use of final is to make sure a method cannot be over ridden if the class is extended.  (Or, in the case of a final class, to make sure that the class can't be extended.)

 

 

Off the top of my head I can't think of an example situation >.<.

 

(Which probably doesn't help you much since I'm sure you knew what the final keyword does.)

Link to comment
Share on other sites

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.