PHP中的抽象静态函数5.3 [英] Abstract Static Function in PHP 5.3

查看:142
本文介绍了PHP中的抽象静态函数5.3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是PHP的相对新手,我正在找出实现一些数据库访问代码的最佳方式。我正在尝试创建一些简单的数据库访问对象 - 每个表都有自己的类,每个类的实例代表表中的一行,你知道这个演练。



我有代码似乎正在工作,但我在网上看到的一些我担心我的方法可能是错误的 - 不知何故。既然我能做到吗?和我该怎么办?是两个不同的问题,我希望一些PHP兽医可以进入。



我目前的策略是创建一个基本级抽象表类,其中包含所有常见的代码和那么每个类代表一个单独的表就可以扩展它。



我的主要关注点是这个代码:

  
抽象类Table {
protected abstract static function get_fields();
protected abstract static function get_primary_key();
protected abstract static function get_table_name();

这个想法是每个实现类将定义字段名称,主键,表名等,然后Table类将使用这些函数填写特定的空格,如下所示:

  
static function insert($ values){
$ field_list = array();
$ value_list = array();
foreach(static :: get_fields()as $ field_name){
if(isset($ values [$ field_name])){
$ field_list [] = $ field_name;
$ value_list [] = self :: escape($ values [$ field_name]);
}
}
$ field_string = join(,,$ field_list);
$ value_string ='。 join(',',$ value_list)。 ;

$ sql =insert into。 static :: get_table_name()。 ($ field_string)values($ value_string);

正如你所看到的,关键是我正在访问那些 static抽象函数通过使用 static :: 来表示。而且据我所知,这是正常的工作!



然而,这个问题表示抽象静态函数仍然不允许5.3。



所以,我想弄清楚该做什么。答案是错误的 - 是抽象静态函数现在被认为是完全合法的PHP代码?我在我的代码中做某些不合适的事情吗?还有另一种方法我应该考虑吗?

解决方案

这是你的例子

 抽象类表实现iTable {

public static function insert(){
$ class = get_called_class();
$ sql ='INSERT INTO'。$ class :: get_class_name();
echo $ sql;
}

}

接口iTable {
static function get_class_name();
}


class ConcreteTable extends Table
{
public function ConcreteTable(){}

static function get_class_name() {
return'ConcreteTable';
}

}

$ t = new ConcreteTable();
$ t :: insert();

这个例子尊重对象范例,你确定它会工作,即使PHP停止支持晚静态绑定(这是PHP的特殊性,我认为)



编辑:两个答案都显示,一个抽象类未知界面以及从其延伸的类。遵循模板模式,即使使用静态函数,这也是可能的(尽管有一个很好的理由给你严格的标准警告)。概念证明:

 抽象类Table 
{
abstract static function get_class_name();
public static function insert(){
printf('INSERT INTO%s',static :: get_class_name());
}

}

class ConcreteTable extends Table
{
public static function get_class_name(){
return'ConcreteTable' ;
}
}

ConcreteTable :: insert();

如果你在这里删除静态关键字,你实际上会变得有用(和标准的做事方式)代码:

 抽象类Table 
{
protected $ table = NULL;
public function insert(){
printf('INSERT INTO%s',$ this-> table);
}

}

class ConcreteTable extends Table
{
protected $ table ='ConcreteTable';
}

$ table = new ConcreteTable();
...
$ table-> insert();


I'm a relative newcomer to PHP, and I'm figuring out the best way to implement some database access code. I'm trying to create some simple database access objects -- each table gets its own class, each instance of the class represents a row in the table, you know the drill.

I have code that seems to be working, but some of what I've seen online makes me worry that my approach may be wrong-headed somehow. And since "Can I do this?" and "SHOULD I do this?" are two different questions, I was hoping some PHP vets could chime in.

My current strategy is to create a base-level abstract Table class that contains all the common code and then have each class representing an individual table extend it.

My main concern is this code:


abstract class Table {
    protected abstract static function get_fields();
    protected abstract static function get_primary_key();
    protected abstract static function get_table_name();

The idea is that each implementing class will define the field names, the primary key(s), the table name, etc., and then the Table class will use those functions to fill in specific blanks, like so:


    static function insert($values) {
        $field_list = array();
        $value_list = array();
        foreach (static::get_fields() as $field_name) {
            if (isset($values[$field_name])) {
                $field_list[] = $field_name;
                $value_list[] = self::escape($values[$field_name]);
            }
        }
        $field_string = join(", ", $field_list);
        $value_string = "'" . join("', '", $value_list) . "'";

        $sql = "insert into " . static::get_table_name() . " ($field_string) values ($value_string)";

As you can see, the key is that I'm accessing those static abstract functions by prefacing them with static::. And as far as I can tell, it's working!

However, the accepted answer to this question indicates that abstract static functions still aren't allowed in 5.3.

So, I'm trying to figure out what to make of this. Is the answer wrong -- are abstract static functions now considered perfectly legal PHP code? Am I doing something inadvisable in my code? Is there another approach I ought to consider?

解决方案

Here's your example

abstract class Table implements iTable {

    public static function insert() {
        $class = get_called_class();
        $sql = 'INSERT INTO '.$class::get_class_name();
        echo $sql;
    }    

}

interface iTable {
    static function get_class_name();
}    


class ConcreteTable extends Table
{
    public function ConcreteTable() {}

    static function get_class_name() {
        return 'ConcreteTable';
    }

}

$t = new ConcreteTable();
$t::insert();

This example respects object paradigm, and you're sure it'll work even if PHP stop support late static bindings (which is a PHP specificity, I think)

Edit: What both answers show is that it's unknown that an abstract class introduces an interface as well for classes extending from it. Following the template pattern, this is possible in PHP even with static functions (albeit for a good reason that gives you strict standard warnings). The proof of concept:

abstract class Table
{
    abstract static function get_class_name();
    public static function insert() {
        printf('INSERT INTO %s', static::get_class_name());
    }    

}

class ConcreteTable extends Table
{
    public static function get_class_name() {
        return 'ConcreteTable';
    }
}

ConcreteTable::insert();

If you remove the static keywords here, you actually will get useful (and a standard way of doing things) code:

abstract class Table
{
    protected $table = NULL;
    public function insert() {
        printf('INSERT INTO %s', $this->table);
    }    

}

class ConcreteTable extends Table
{
    protected $table = 'ConcreteTable';
}

$table = new ConcreteTable();
...
$table->insert();

这篇关于PHP中的抽象静态函数5.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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