Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
$variable->;的故事是什么;PHP中的一些东西?_Php_Oop - Fatal编程技术网

$variable->;的故事是什么;PHP中的一些东西?

$variable->;的故事是什么;PHP中的一些东西?,php,oop,Php,Oop,我已经见过很多这样的应用,特别是在SimpleXML中 这是: $row->unixtime 就跟这样做一样 $row[unixtime] 这叫什么,为什么/如何使用?不,它们不一样。是关于 ->表示访问对象成员。例如: class Test { public $foo; public $blah; } $test = new Test; $test->foo = 'testing'; []是一个数组访问操作符,由true数组使用。如果对象实现接口,也可以使用它: c

我已经见过很多这样的应用,特别是在SimpleXML中

这是:

$row->unixtime
就跟这样做一样

$row[unixtime]

这叫什么,为什么/如何使用?

不,它们不一样。是关于

->
表示访问对象成员。例如:

class Test {
  public $foo;
  public $blah;
}

$test = new Test;
$test->foo = 'testing';
[]
是一个数组访问操作符,由true数组使用。如果对象实现接口,也可以使用它:

class Test2 implements ArrayAccess {
  private $foo = array();

  public function offsetGet($index) { return $this->foo[$index]; }
  // rest of interface
}

$test2 = new Test2
$test2['foo'] = 'bar';

完全不同

第一个,
$row->unixtime
表示您正在访问类
$row
的对象/实例的公共变量
$unixtime
。这是面向对象编程

例如:

class example{
  public $unixtime = 1234567890;
}

$row = new example();
echo $row->unixtime; // echos 1234567890
第二个是获取数组
$row
的键
'unixtime'
。这称为关联数组。例如:

$row = array(
          'unixtime' => 1234567890
       );
echo $row['unixtime']; // echos 1234567890
$row = array(
          'unixtime' => 1234567890
       );
$row = (object)$row;
echo $row->unixtime; // echos 1234567890
通过使用
(数组)
(对象)
强制转换,可以轻松地在对象和数组之间进行转换。例如:

$row = array(
          'unixtime' => 1234567890
       );
echo $row['unixtime']; // echos 1234567890
$row = array(
          'unixtime' => 1234567890
       );
$row = (object)$row;
echo $row->unixtime; // echos 1234567890
离题:我实际上错过了2月份的unix纪元时间1234567890。

$row是一个对象。unixtime是该对象的一个属性

$row[unixtime] // I hope you meant $row['unixtime'];
$row是一个(关联)数组。unixtime是该数组中的一个键

问“物体是什么”有点模糊

  • (维基百科)

开始OOP并不是一项简单的任务。学习语法和细微差别需要一段时间,理解优点需要一段时间,实际有效地使用它需要几年时间(可以说)

让你的答案简短而甜蜜

$row->unixtime
这是一个对象

$row[unixtime]

这是一个数组

它很可能是从中提取的另一个习惯用法,实际上就是PHP的编写语言。PHP的许多特性、语法和运算符,甚至许多PHP的本机函数都源于C