2017-07-25 59 views
1

我是新來的面向對象編程在PHP中。 我用數組屬性做了一個簡單的訂單類。 orderLength方法不起作用。我得到一個錯誤。在php中獲取數組長度

調用未定義的方法令::計數()

PHP:

<?php  
    class Order { 
     private $order = array(); 

     public function setOrder($wert) { 
      foreach($wert as $value) { 
       $this -> order[] = $value; 
      } 
     } 

     public function orderLength() { 
      $length = $this -> count(order); 
      return $length; 
     } 

     public function returnOrder() { 
      $value = $this -> order; 
      return $value; 
     } 
    } 

    $order = new Order; 
    $order -> setOrder(array('Book1','Book2','Book3','Book4')); 

    foreach($order->returnOrder() as $value) { 
     echo $value."<br>"; 
    } 

    echo "The order Length is: ".$order->orderLength(); 
+0

http://php.net/manual/en/class.countable.php – Scuzzy

+4

沒有'count'方法 – ArtOsi

回答

0

相反的$this->count(order)你可以嘗試用:

$length = count($this->order); 
0

計數是一種方法,而不是一個類屬性,試試這個:

$length = count($this->order); 
3

只需返回$訂單的計數:

public function orderLength() { 
     return count($this->order); 
    } 
0

只是你miske該行

$this -> count(order); 

你的類有作爲計數,以便去除任何一個成員函數:

this -> 
0

您尚未爲班級定義方法計數。你已經做到了這一點,看起來你正在調用你的班級的count方法,並將order作爲參數。

$length=count($this->order); 
0

將值賦予變量順序,然後編寫獲取計數的代碼。

$order = $order -> setOrder(array('Book1','Book2','Book3','Book4')); 
echo "The order Length is: ".$order->orderLength();