hkothari Posted December 19, 2009 Share Posted December 19, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/185703-abstract-fields-in-abstract-class/ Share on other sites More sharing options...
KevinM1 Posted December 19, 2009 Share Posted December 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185703-abstract-fields-in-abstract-class/#findComment-980590 Share on other sites More sharing options...
hkothari Posted December 19, 2009 Author Share Posted December 19, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/185703-abstract-fields-in-abstract-class/#findComment-980595 Share on other sites More sharing options...
Daniel0 Posted December 19, 2009 Share Posted December 19, 2009 You can use late static bindings as of PHP 5.3.0. Quote Link to comment https://forums.phpfreaks.com/topic/185703-abstract-fields-in-abstract-class/#findComment-980624 Share on other sites More sharing options...
KevinM1 Posted December 19, 2009 Share Posted December 19, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/185703-abstract-fields-in-abstract-class/#findComment-980627 Share on other sites More sharing options...
Daniel0 Posted December 19, 2009 Share Posted December 19, 2009 If you have it as a class property, just use that instead of using a method parameter. You would have to change the visibility to protected though. Quote Link to comment https://forums.phpfreaks.com/topic/185703-abstract-fields-in-abstract-class/#findComment-980628 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.