【问题标题】:How to call a function correctly in PHP OOP如何在 PHP OOP 中正确调用函数
【发布时间】:2014-11-19 15:45:10
【问题描述】:

我正在尝试在 PHP 中学习更好的 OOP,并且我已经尝试解决这个问题好几个小时了,需要一些帮助。我使用的是 php 5.4,所以我相信我可以使用后期静态绑定。
我有一个名为 DatabaseObject (database_object.php) 的类,它有一个名为 create 的函数,如下所示:

    public function create() { 
      echo "in Create() ";
      global $database; echo "<br> table: ".static::$table_name;
      $attributes = $this->sanitized_attributes(); 
      $sql = "INSERT INTO " .static::$table_name." (";
      $sql .= join(", ", array_keys($attributes));
      $sql .= ") VALUES ('";
      $sql .= join("', '", array_values($attributes));
      $sql .= "')"; echo $sql; 
      if($database->query($sql)) {
        $this->id = $database->insert_id();
        return TRUE;
      } else {
        return FALSE;
      }
    }

我从我的 Cart 类(在一个名为 cart_id.php 的文件中)调用它,该类在一个名为 add_to_cart() 的函数中扩展 DatabaseObject,如下所示:

    public function add_to_cart($cart_id,$isbn) { 
      global $database;
      $isbn = $database->escape_value($isbn);
      $amazon = Amazon::get_info($isbn);
      //get cart id if there is not one
      if (empty($cart_id)) {echo " getting cart_id";
        $cart_id = static::get_new_cart_id();
      }
      if(!empty($amazon['payPrice']) && !empty($isbn)) {
        echo "<br> getting ready to save info";
        $cart = new Cart();
        $cart->price = $amazon['payPrice'];
        $cart->qty = $amazon['qty'];
        $cart->cart_id =$cart_id;
        $cart->isbn = $isbn;
        if(isset($cart->cart_id)) { 
          echo " Saving...maybe";
          static::create();
        }
      }

      return $amazon;
    }

静态的:create();正在调用该函数,但是当它到达时

$attributes = $this->sanitized_attributes();

它没有调用我的 DatabaseObject 类中的 sanitized_attributes 函数

    protected function sanitized_attributes() {
      echo "<br>in Sanatized... ";
      global $database;
      $clean_attributes = array();
      //Sanitize values before submitting
      foreach($this->attributes() as $key=>$value) {
        $clean_attributes[$key] = $database->escape_value($value);
      }
      return $clean_attributes;
    }

属性是

    protected function attributes() {
      //return get_object_vars($this);
      $attributes = array();
      foreach (static::$db_fields as $field) {
        if(property_exists($this, $field)) {
          $attributes[$field] = $this->$field;
        }
       }
       return $attributes;
     }

我得到 echo "in create()" 以及 echo "table ".static:table_name,它确实显示了要保存到的正确表。我没有得到 echo $sql,也没有得到“In Sanitized”。如果我取出 static:create() 行,它会继续运行而不会出现问题,并在我的 return $amazon 语句中显示信息。 我的问题是,我应该如何从我的 add_to_cart() 函数中正确调用 create 函数? 如果您要否决我的问题,您能否解释一下为什么我不会再次重复相同的错误?谢谢!

【问题讨论】:

  • 请以正确的缩进开始您的 OOP 冒险。
  • 我发现很难在短时间内和没有太多上下文的情况下遵循您的代码,但是...您确定您正确使用了 static 关键字吗?除了声明静态成员和方法之外,它在php中是一个相对“新”的东西,更像是一个修复(它应该从基类到达类层次结构的顶部以检索最派生的方法或值)。 .. 为什么不只是“this->create()”?在您的基类中定义一个抽象的“get_table”方法并让派生类实现它,在创建时只需执行“this->get_table()”就完成了...

标签: php oop


【解决方案1】:

由于您正在静态调用 create,因此您必须静态调用同一类的任何其他方法,因为您使用的不是类的“实例”,而是它的静态版本。

我不知道其余代码的结构,但您可以将 static::create() 更改为 $this-&gt;create() 并将 create 内部的静态调用更改为调用 $this 或将 $this-&gt;sanitized_attributes() 更改为 static::sanitized_attributes()

另外,您应该避免使用全局变量。由于您使用 OOP,因此您应该练习正确的依赖注入并将这些全局变量传递到您的类中,而不是使用 global $blah

【讨论】:

  • 感谢您的回答 DigitalFiz,我实现了 static::sanitized_attributes(因为当我尝试 $this->create 时它不起作用)并且我在 Sanitized 中得到了我的回声。我的全局传递了连接信息,那么我应该如何将它传递到我的班级??
  • 我假设如果 $this->create 不起作用,那么您可能是静态调用 add_to_cart ?就像我说的那样,我认为您可能已经删减了大部分代码,以便任何人真正说出问题所在。如果 add_to_cart 是静态调用的,那么同一个类中的每个方法在通过 create 方法使用时也需要静态调用。这是混合静态/非静态方法时您可能会感到困惑的一种方式。在我看来,你真的应该避免静态调用,除非它是为了做简单或特定的事情。
  • 你是对的,我用 $result = Cart::add_to_cart($cart_id, $isbn); 来称呼它最好的方法是什么?
  • 也许需要修改问题以更好地了解您要完成的工作?我认为你把事情搞混了一点。看起来您正在对单个项目和整个购物车使用 Cart 类。也许您应该创建一个 Item 类来存储有关该项目的信息,然后将 Cart 类全部设为静态,因为我假设您只是使用它与数据库进行交互。因此,您将首先生成 Item 然后调用 add_to_cart 静态传递 Item 的实例。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 2010-12-26
  • 1970-01-01
  • 1970-01-01
  • 2015-12-14
  • 1970-01-01
相关资源
最近更新 更多