seventheyejosh Posted September 11, 2009 Share Posted September 11, 2009 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 Quote Link to comment Share on other sites More sharing options...
KevinM1 Posted September 11, 2009 Share Posted September 11, 2009 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. Quote Link to comment Share on other sites More sharing options...
corbin Posted September 11, 2009 Share Posted September 11, 2009 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.) Quote Link to comment 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.