Jump to content

[SOLVED] I really don't understand abstract classes


Demonic

Recommended Posts

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?   

Oh wow  ;D, 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?

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.

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.

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

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.