PHP - 扩展类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11194506/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:57:22  来源:igfitidea点击:

PHP - Extending Class

phpclassextendsobject-oriented-database

提问by TheLettuceMaster

I've done lots and lots of code in PHP that is object-oriented, but up until now, all of my classes have been, "singular", I suppose you can call it. I am in the process of changing several classes (that have 5 or so identical methods) to extend one class (to rid myself of duplicate code). I am running into a few issues.

我已经用 PHP 编写了很多面向对象的代码,但到目前为止,我所有的类都是“单一的”,我想你可以称之为。我正在更改几个类(有 5 个左右相同的方法)以扩展一个类(以消除重复的代码)。我遇到了一些问题。

I am trying to access a method in a parent class, but you can see the issue.

我正在尝试访问父类中的方法,但您可以看到问题。

Parent class:

父类:

 class DatabaseObject { 

     public static function find_all() {
        return self::find_by_sql("SELECT * FROM " . self::$table_name);
    }
}

Child Class:

儿童班:

class Topics extends DatabaseObject {

    protected static $table_name = "master_cat";
    protected static $db_fields = array('cat_id', 'category');
    public $cat_id;
    public $category;

  }

Code trying to access all info from this table from php/html file:

尝试从 php/html 文件访问此表中所有信息的代码:

$topics=Topics::find_all();

foreach($topics as $topic):
    echo $topic->category;
endforeach; 

As you can see, Most of the code has not been merged to the new way of doing things. I need to change the self::$table_name which no longer works in the new way I am doing things. I will have about 5 Classes extending this object, so what is the best way of coding this so I can access different tables with one method (rather than including this exact find_all() method in 5 different classes.

如您所见,大部分代码尚未合并到新的处事方式中。我需要更改 self::$table_name ,它不再以我做事的新方式工作。我将有大约 5 个类扩展这个对象,那么什么是最好的编码方式,这样我就可以用一种方法访问不同的表(而不是在 5 个不同的类中包含这个确切的 find_all() 方法。

回答by Matthijs

You could try late static binding as mentioned below, or a singleton solution should work as well:

您可以尝试如下所述的后期静态绑定,或者单例解决方案也应该有效:

<?php
abstract class DatabaseObject {
  private $table;
  private $fields;

  protected function __construct($table, $fields) {
    $this->table = $table;
    $this->fields = $fields;
  }

  public function find_all() {
    return $this->find_by_sql('SELECT * FROM ' . $this->table);
  }
}

class Topics extends DatabaseObject {
  private static $instance;

  public static function get_instance() {
    if (!isset(self::$instance)) {
      self::$instance = new Topics('master_cat', array('cat_id', 'category'));
    }

    return self::$instance;
  }
}

Topics::get_instance()->find_all();

回答by Jeremy Livingston

You should look into the concept of Late Static Bindingin PHP. This allows you to access a static constant or function from the class that was called.

您应该研究PHP中后期静态绑定的概念。这允许您从被调用的类访问静态常量或函数。