Jump to content

Recommended Posts

So, I have an abstract class that has a function called "create" which inserts stuff into the database if they are in the an array which is supplied as a parameter. It's a static function defined like this:

/**
 * create($fields)
 * Creates a thing in the database using the supplied array of fields.
 * @param $fields the fields with keys as their column and values as their rows.
 * @param $validFields an array with a list of valid fields.
 * @param $tableName since this is a static function we need the table to insert to.
 */
static function create(array $fields, $tableName, $validFields)
{
	$keys = "";
	$values = "";
	foreach($fields as $key => $value)
	{
		if(in_array("$key", $validFields) && !empty($value))
		{
			$keys .= "$key,";
			$values .="'$value',";
		}
	}
	$keys = rtrim($keys, ",");
	$values = rtrim($values, ",");

	$mysql = new Mysql_Wrapper();
	return $mysql->query("INSERT INTO $tableName($keys) VALUES($values)");
}

and this works, it's in the main abstract class. Now to implement this I make a "create" function in the inheriting class and call the create function with the parameters, but I was wondering, since this is an abstract class, is there some way I can make some constant abstract fields in the class, and implement them in the inheriting class in order to remove the need of redefining the function in the inheriting class just to add the validFields and the tableName. Is there anyway to do this?

Link to comment
https://forums.phpfreaks.com/topic/185703-abstract-fields-in-abstract-class/
Share on other sites

You don't need to redefine the method in the child class.  Simply make it non-static in the abstract base class.  That's the way abstract classes are typically used - non instantiated classes that contain all of the executable base functionality their children can use.  This makes the child classes simple to create as one doesn't have to redefine within them what's already present in the parent.

Well, it is called statically though because it's just doing one thing that's it, also each child class has different values for the two parameters so that's why I have to redefine it. So for example if the class was like a Cat and it inserted it into the table "cats" I redefine it in order to do this:

 

static function create(array $fields)
{
    $validFields = array("name", "somethingelse");
    parent::create($fields, "cats", $validFields);
}

So not making it static wouldn't really solve my problem.

But in each of the child classes though there could be a constant for the validFields and tableName so is there a way to access these constants in the abstract class?

You don't need to make it static to do that.  Simply override it in the child class:

 

abstract class DB
{
   public function create(array $fields, $tableName, $validFields)
   {
      $keys = "";
      $values = "";
      foreach($fields as $key => $value)
      {
         if(in_array("$key", $validFields) && !empty($value))
         {
            $keys .= "$key,";
            $values .="'$value',";
         }
      }
      $keys = rtrim($keys, ",");
      $values = rtrim($values, ",");

      $mysql = new Mysql_Wrapper();
      return $mysql->query("INSERT INTO $tableName($keys) VALUES($values)");
   }
}

class Cat extends DB
{
   private $tableName = "cats";

   public function create(array $fields)
   {
      parent::create($fields, $this->tableName, /* whatever $validFields are supposed to be */);
   }
}

 

I could be misreading your question, but it seems like you merely want to invoke the abstract class' method with parameters specific to a child class.

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.