目前我正在使用PHP中的简单OOP脚本,它需要比较数组的ID和DATE,并按正确的顺序对它们进行排序。
我想知道为什么第一个类中的构造函数没有正确传递$ elements数组。
我得到的错误:
注意:未定义的变量:第58行/Applications/XAMPP/xamppfiles/htdocs/strategy-pattern-.php中的元素
可捕获的致命错误:传递给ObjectCollection :: __ construct()的参数1必须是数组类型,给定null,在第58行调用...并在第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);
?>
我知道在某个地方可能只有一个新手的错误,但我真的很好奇我做错了什么哈哈。
提前致谢! :)
答案 0 :(得分:3)
在脚本的底部,您有:
$collection = new ObjectCollection($elements);
但是,未定义$elements
变量。这就是您收到错误的原因。
具体错误与您在类构造函数中使用type declaration要求使用&#39;数组&#39;通过。在向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定义为脚本底部的数组,要么在创建{{1}时传入数组对象。
ObjectCollection
答案 1 :(得分: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
答案 2 :(得分:1)
您已在类中声明了elements
变量,而不是
$elements = array(
array('id' => 2, 'date' => '2017-01-01',),
array('id' => 1, 'date' => '2017-02-01'));
class ObjectCollection
{