【发布时间】:2017-05-23 22:41:27
【问题描述】:
class Item
{
private $_SomeArray = array();
private $elementOfanArray;
function __construct()
{
$this->_SomeArray[0]="default message";
$this->elementOfanArray=0; //I am trying to assign empty value to an var
}
public function getArray()
{
return $this->SomeArray; //function returning whole array
}
public function setArray(elementOfanArray) //function for adding new element to an array and checking if not empty
{
if(!elementOfanArray)
{
}
else
{
array_push($_SomeArray, elementOfanArray);
$elementOfanArray = 0; //I am trying to assign value after addding element to an array
}
}
function __destruct(){
$this->_SomeArray;
$this->elementOfanArray;
}
}
这是我的代码,但现在我想知道使用“array_push”是否是正确的选择,因为它会被调用大约 20 次。 函数调用的数量不是恒定的,所以如果值更少..我想从数组中删除它们或立即转储它们。
我得到的另一个问题是我应该如何将我的元素从这个类中取出,有没有办法让整个数组的值在这个类之外?
我写的对吗?
【问题讨论】:
-
如果您在该类或函数中从您的类中调用变量,则需要使用
$this->
标签: php arrays function class variables