Demonic Posted November 7, 2007 Share Posted November 7, 2007 Can someone explain it to me, its just something I feel is hard to learn. Quote Link to comment Share on other sites More sharing options...
trq Posted November 7, 2007 Share Posted November 7, 2007 What part don't you understand exactly? They are pretty straight forward, almost like interfaces but can (and usually do) impliment some functionality. I hate to go down the cat dog road, but it really is the easiest way. Both a cat and a dog are an animal, and they can both eat, so we could define an abstract class that impliments the functionality for eating. They also both make noise, one however barks while the other meows. Therefore, we need to define an abstract method for making noise, bt leave the implimentation to the child classes. By making the class abstract you ensure that it cannot be instantiated directly. By creating the abstract method you ensure that any child class will impliment such functionality. <?php abstract class animal { public function eat() { echo "eating"; } abstract public function makenoise(); } ?> Now, if we want to make a dog object inherit from animal, we know that it will already have an eat() method implimented, but we MUST impliment the makenoise() functionality. <?php class dog extends animal { public function makenoise() { echo "woof woof"; } } $dog = new dog(); $dog->eat(); $dog->makenoise(); ?> Does that help at all? Quote Link to comment Share on other sites More sharing options...
Demonic Posted November 7, 2007 Author Share Posted November 7, 2007 Oh wow , so basically a abstract class is like a class that contains the same functionality, but certain things respond in a different, like you said both can eat but when they make noise's they do it in a different way. i.e, a MySQL Class <?php //Basic Example By Lamonte(Demonic) of cleanscript.com abstract class SQL { public $sql_user; public $sql_pass; public $sql_host; public $sql_db; private $error_msg; public function push_error() { if(strlen($error_msg) > 0) { echo $error_msg; } } abstract function connect($sql_user,$sql_pass,$sql_host,$sql_db){} } class s_mysql extends SQL { function connect($sql_user,$sql_pass,$sql_host,$sql_db) { if(!@mysql_connect($sql_host,$sql_user,$sql_pass)) { $this->error_msg .= mysql_error() . ", "; } if(!@mysql_select_db($sql_db)) { $this->error_msg .= mysql_error() . ", "; } } } class s_mysqli extends { function connect($sql_user,$sql_pass,$sql_host,$sql_db) { $link = mysqli_connect($sql_host, $sql_user, $sql_pass, $sql_db); if( !$link ) { $this->error_msg .= mysqli_connect_error() . ", "; } } } $sql = new s_mysqli; $sql->connect("localhost","test_test","madeup_password","test_test"); $sql->push_error(); unset($sql); $sql = new s_mysql; $sql->connect("localhost","test_test","madeup_password","test_test"); $sql->push_error(); ?> Like that? Quote Link to comment Share on other sites More sharing options...
trq Posted November 7, 2007 Share Posted November 7, 2007 Yeah, something like that. Basically, an abstract class cannot be instantiated but can implement some common functionality which all children will inherit. Abstract methods are there to define functionality that must exist within each class, but the actual implementation is left up to the child class. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 7, 2007 Share Posted November 7, 2007 The code you posted won't work. You defined $error_msg as private, but you try to use it in a child class. Some people use interfaces where they should have used abstract classes. Interfaces and abstract classes is not the same and is not intended to be used like the same. Also, check the (quite long) example I posted in one of your other topics: http://www.phpfreaks.com/forums/index.php/topic,166483.msg734737.html#msg734737 I used abstract classes there too. Also note that you can't instantiate an abstract class. Quote Link to comment Share on other sites More sharing options...
Demonic Posted November 7, 2007 Author Share Posted November 7, 2007 The code you posted won't work. You defined $error_msg as private, but you try to use it in a child class. don't really care if it worked or not it was an example hense everyone posting examples here right?... Quote Link to comment Share on other sites More sharing options...
teng84 Posted November 7, 2007 Share Posted November 7, 2007 The code you posted won't work. You defined $error_msg as private, but you try to use it in a child class. don't really care if it worked or not it was an example hense everyone posting examples here right?... danielo is telling you that you cant declare $error_msg as private if you will use it in the other class Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted November 7, 2007 Share Posted November 7, 2007 Indeed, in order to use it in child classes it must be declared as protected. Quote Link to comment Share on other sites More sharing options...
Demonic Posted November 8, 2007 Author Share Posted November 8, 2007 Like I said...IT WAS AN EXAMPLE...ffs. Making it seem like I was trying very hard to create an example that I never was going to test just seeing if my THEORY was correct. Get it? Got it? Good. Quote Link to comment Share on other sites More sharing options...
aschk Posted November 8, 2007 Share Posted November 8, 2007 Ah, hate to be sticking the knife in here, but it's very important to make sure you declare internal variables correctly. I realise you've posted an example but for clarity's sake I think they have a valid point. Just worth pointing out to n00bies that when you inherit from another class, ONLY protected and public variables are inherited. Any in the base class that are private are NOT accessible. Either provide a protected or public accessor function getErrorMsg(), allowing you to grab the internal base variable $error_msg. It's a common pitfall so definitely mention-worthy. Quote Link to comment Share on other sites More sharing options...
Demonic Posted November 8, 2007 Author Share Posted November 8, 2007 Ah, hate to be sticking the knife in here, but it's very important to make sure you declare internal variables correctly. I realise you've posted an example but for clarity's sake I think they have a valid point. Just worth pointing out to n00bies that when you inherit from another class, ONLY protected and public variables are inherited. Any in the base class that are private are NOT accessible. Either provide a protected or public accessor function getErrorMsg(), allowing you to grab the internal base variable $error_msg. It's a common pitfall so definitely mention-worthy. Okay thanks for the tip. 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.