2017-06-16 26 views
0

目前我正在使用PHP中的一個簡單的OOP腳本,它需要比較數組的ID和DATE並按正確的順序對它們進行排序。如何正確構建PHP中的類中的數組?

我想知道爲什麼我的第一個類的構造函數沒有正確傳遞$ elements數組。

我得到的錯誤:

Notice: Undefined variable: elements in /Applications/XAMPP/xamppfiles/htdocs/strategy-pattern-.php on line 58

Catchable fatal error: Argument 1 passed to ObjectCollection::__construct() must be of the type array, null given, called in ... on line 58 and defined in ... on line 12

代碼:

<?php 

class ObjectCollection 
{ 

var $elements = array(
array('id' => 2, 'date' => '2017-01-01',), 
array('id' => 1, 'date' => '2017-02-01')); 

var $comparator; 

    function __construct(array $elements) 
    { 
     $this->elements = $elements; 
    } 

    function sort() 
    { 
     if (!$this->comparator) { 
      throw new \LogicException('Comparator is not set'); 
     } 

     uasort($this->elements, [$this->comparator, 'compare']); 

     return $this->elements; 
    } 

    function setComparator(ComparatorInterface $comparator) 
    { 
     $this->comparator = $comparator; 
    } 
} 

interface ComparatorInterface 
{ 
    function compare($a, $b); 
} 

class DateComparator implements ComparatorInterface 
{ 
    function compare($a, $b) 
    { 
     $aDate = new \DateTime($a['date']); 
     $bDate = new \DateTime($b['date']); 

     return $aDate <> $bDate; 
    } 
} 

class IdComparator implements ComparatorInterface 
{ 
    function compare($a, $b) 
    { 
     return $a['id'] <> $b['id']; 
    } 
} 

$collection = new ObjectCollection($elements); 
$collection->setComparator(new IdComparator()); 
$collection->sort(); 
echo "Sorted by ID:\n <br>"; 
print_r($collection->elements); 
$collection->setComparator(new DateComparator()); 
$collection->sort(); 
echo "<br>Sorted by date:\n <br>"; 
print_r($collection->elements); 

?> 

我知道有可能只是一個新手的錯誤的地方,但我是我做的真的很好奇自己做錯了什麼,哈哈。

在此先感謝! :)

+1

嘛'var'一直沒有在相當一段時間內使用的屬性定義。你使用什麼手冊? PHP4 – RiggsFolly

回答

3

在腳本的底部出現了:

$collection = new ObjectCollection($elements); 

然而,$elements變量沒有定義。這就是你遇到錯誤的原因。

具體錯誤與您在類構造函數中使用type declaration需要傳遞「數組」的事實有關。在向php添加類型聲明之前,php運行時引擎不關心你傳遞了哪些變量,只要你將一些變量傳遞給一個函數或方法所需的參數。

作爲另一個答案也指出,我們很多人都假設你的位置 -

var $elements = array(
    array('id' => 2, 'date' => '2017-01-01',), 
    array('id' => 1, 'date' => '2017-02-01')); 

從來就不是是類裏面。據說,這樣做會創建並初始化$ elements類變量,這是一種在OOP中有許多用途的有效技術。然而,使用的語法是過時的,如果你真的想初始化類變量在創建對象的時候設定值時,你應該使用包含語法variable visibility關鍵字,如:

protected $elements = array(
    array('id' => 2, 'date' => '2017-01-01',), 
    array('id' => 1, 'date' => '2017-02-01')); 

在結論,對您的問題的回答是,您應該將$ collection定義爲腳本底部的數組,或者在創建ObjectCollection對象時傳遞數組。

$collection = new ObjectCollection(array(
    array('id' => 2, 'date' => '2017-01-01'), 
    array('id' => 1, 'date' => '2017-02-01')); 
+0

現在這應該是被接受的答案 – Akintunde007

+0

現在我明白了,謝謝! –

+0

很高興提供幫助。由於您似乎正在進行一些學習練習,因此可以理解的是,您將多個類放在同一個腳本中,但在專業/真實世界的PHP中,您總是要將您的類放在單獨的腳本中,並進行相應的命名。看到這個標準「http://www.php-fig.org/psr/psr-1這樣做是非常重要的,現在PHP有命名空間和組件庫,以及執行依賴管理並生成要使用的自動加載器的作曲家工具在您的項目中 – gview

2
class ObjectCollection 
{ 
    // define as property 
    private $elements; 

    private $comparator; 

    function __construct(array $elements) 
    { 
     $this->elements = $elements; 
    } 

    function sort() 
    { 
     if (!$this->comparator) { 
      throw new \LogicException('Comparator is not set'); 
     } 

     uasort($this->elements, [$this->comparator, 'compare']); 

     return $this->elements; 
    } 

    function setComparator(ComparatorInterface $comparator) 
    { 
     $this->comparator = $comparator; 
    } 
} 


... 

// you need to define $elements to pass 
$elements = array(
array('id' => 2, 'date' => '2017-01-01',), 
array('id' => 1, 'date' => '2017-02-01')); 

// them to the constructor 
$collection = new ObjectCollection($elements); 



// the way you did it, your $elements definition was in class scope so you got the error they are "NULL"/Not defined 
+0

是的,但你必須失去var關鍵字。 – gview

+0

絕對正確 –

+2

解釋爲什麼這回答這個問題會有幫助。僅有代碼的答案並不真正具有啓發性。 – trincot

1

您已聲明的類內elements變量,而不是外界

$elements = array(
array('id' => 2, 'date' => '2017-01-01',), 
array('id' => 1, 'date' => '2017-02-01')); 

class ObjectCollection 
{ 
+0

@trincot在PHP中也不需要'var' – MaxZoom

相關問題