如何处理具有许多参数的函数的继承? [英] How to handle inheritance of a function with many parameters?

查看:66
本文介绍了如何处理具有许多参数的函数的继承?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在PHP for MySQL中编写数据库交互层。但我认为这是一个普遍的OOP问题(见最后一行)。

I am writing a database interaction layer in PHP for MySQL. But I think this is a general OOP question (see last line).

我有一个基本的dbTables类。
并且它有一个

I have a basic dbTables class. And it has a

public static function getBy($method='name', $value) {
        // Gets flat table array of db Tables matching by $method == $value
        // later could implement some lookup tables.
        $allowed = array('name');
        $query_format = SHOW TABLES LIKE '%s'";
        if(in_array($method,$allowed)) {
            dbConnection::connect(MAIN_DB); // makes db connection
            $safe_value = mysql_real_escape_string($value);
            // MAY want to change this query to a SCHEMA query in CHILD classes
            $sql = sprintf($query_format,$safe_value);
            // e.g. $sql = "SHOW TABLES LIKE '$safe_value'";
            $result = mysql_query($sql);
            if($result === false) {
                debug::add('errors', __FILE__, __LINE__, __METHOD__,"Query Error for query '$sql'. MySQL said: " . mysql_error());
            }
            while($row = mysql_fetch_row($result)) {
                $db_table = new static($row[0]); // creates instance of $this class
                $object_array[] = $db_table; // add to $object_array for return value
            }
        } else {
            debug::add('errors',__FILE__, __LINE__, __METHOD__, ' - Wrong method: ' . $method . '. Currently allowed: ' . print_r($allowed,true));
            return false;
        }
    return $object_array;
    // END public static function getBy($method='name', $value)
    }

但是子类会有不同的查询来获取信息。他们将有其他允许的$方法进行搜索。

But child classes will different queries to get information. They will have other allowed $methods for searching.

这是我的解决方案,但我不知道这是不是很好的做法,如果以后会导致更多的痛苦。我可以创建一组私有静态属性,而不是在每个子类中覆盖此函数,该属性将充当函数的修饰符。

Here is my solution, but I don't know if it is good practice and if it will lead to more pain later. Rather than override this function in every child class, I can create a set of private static properties that will act as modifiers for the function.

如下所示:

    protected static $get_by_methods = array('name'); // array('name','id','frontend_name'…) in CHILDREN
    protected static $get_by_query_format = "SHOW TABLES LIKE '%s'"; // for sprintf. Changes in children
    protected static $get_by_handles_arrays = false; // true in CHILDREN
    protected static $get_by_query_format_array = " SELECT * FROM %s` WHERE `$method` IN ($safe_values)"; // used in CHILDREN ONLY

    public static function getBy($method, $value) {
        $allowed = self::$get_by_methods;
        $query_format = self::$get_by_query_format;
        $handle_arrays = self::$get_by_handles_arrays; // false here,,, true in children
        $query_format_array = self::$get_by_query_format_array; // used in children
        if(is_array($value) && $handle_arrays === true) {
            return false; // in child class, $handle_arrays can be set to true outside of function
                // without rewriting function. just change the static property
        }
        if(in_array($method,$allowed)) {
            dbConnection::connect(MAIN_DB);
            if(!is_array($value)) { // handle string values
                $safe_value = mysql_real_escape_string($value);
                $sql = sprintf($query_format,$safe_value);
            } else {
                // arrays used only in children
e.g. 
$safe_values = mysql_real_escape_string(implode(',',$value)); // convert to string
                $sql = sprintf($query_format_array,$safe_values); // used in children
            }
            $result = mysql_query($sql);
            if($result === false) {
                debug::add('errors', __FILE__, __LINE__, __METHOD__,"MySQL Error num " . mysql_errno() . " for query [$sql] - MySQL said: " . mysql_error());
            }
            while($row = mysql_fetch_row($result)) {
                $db_table = new dbTables($row['name']);
                $object_array[] = $db_table;        
            }
        } else { // if bad method chosen above
            debug::add('errors',__FILE__, __LINE__, __METHOD__, ' Wrong method: ' . $method . '. Must use one of these: ' . print_r($allowed,true));
            return false;
        }
    return $object_array;
    // END public static function getBy($method='name', $value)
    }

总而言之,这样做可以让我永远不会覆盖getBy()方法。我只需要覆盖随之而来的受保护的静态属性。对于DRY(不要重复自己),这似乎很好。我只需要一遍又一遍地编写4行代码而不是20行代码。但我是新手,不知道这可能是一个可怕的错误,原因是其他原因。

To sum up, doing this will allow me to never override the getBy() method. I will only have to override the protected static properties that go with it. For DRY (don't repeat yourself), this seems good. I will only have to write 4 lines of code instead of 20+ over and over again. But I am new at this and don't know if this might be a horrible mistake for some other reason.

继承优先权是否安全和良好做法方法并将其放入辅助属性?

Is it safe and good practice to take the inheritance overriding out of the methods and put it into helper properties?

推荐答案

 $allowed = array('name');
 $query_format = SHOW TABLES LIKE '%s'";

似乎属于班级属性,因为您需要在子类中访问它,以覆盖行为。

seems to belong to class property, since you need to access this in your child classes to, to overwrite the behavior.

并且在您的子类中,您可以覆盖行为或者可以使用相同的使用父关键字

and in your child classes you can overwrite the behavior or can use the same by using parent keyword like

$allowed = parent::$allowed;

现在$ allowed将具有从父项继承的值。

now $allowed will have the value inherited from the parent.

该方法也是如此。你想调用父方法,然后使用父关键字。

same goes for the method. if you want to call the parent method then use the parent keyword.

parent :: getBy();在你的子类中。永远记住干(不要重复自己)在你的情况下,你在你的父类和子类中重复代码。所以使用parent来调用子类中的父方法。例如在你的子类中

parent::getBy(); in your child classes. always remember the DRY(don't repeat yourself) principle. in your case you are repeating the code in your parent and child class. so instead use parent to call the parent method in child class. for example in your child class

public function getBy()
{
    parent::getBy();
}

没有孩子getBy()将从父继承。你唯一应该覆盖的是class属性。

now the child getBy() will inherit from parent. the only thing you should override is class property.

这篇关于如何处理具有许多参数的函数的继承?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆