3

PHP 原来是这么神奇的语言吗?

 2 years ago
source link: https://www.v2ex.com/t/839895
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

V2EX  ›  程序员

PHP 原来是这么神奇的语言吗?

  darklinden · 1 天前 · 3530 次点击

今天一个同事在吐槽 lua 不好使,然后发我一段代码

locla test = {}
test['a'].b = 0

我:“??? a 对应的对象没有初始化直接访问 b 属性不是崩崩崩?”

回:“js 和 php 都能这么写啊...”

我:“php 我写的少,js 这不是典型的 property of undefined 么?正常语言但凡有一个不报错的都见鬼了”

于是人回了我一段

var test = {};
test['hasOwnProperty'].b = 0

我:“... hasOwnProperty 是特么的通用函数,有对象的好么?你写段 php ”

$test = array();
$test["a"]["b"] = 1024;
print($test);

这见鬼的代码竟然不报错...

第 1 条附言  ·  19 小时 19 分钟前

感谢指出,手打代码手误了…

其实比较纠结的在于其他语言一般二维数组也好,二维字典也好,一级需要先生成对象的,而 php 这神操作,二维数组的一维对象不存在的情况下直接默认对象了…我只能认为这是神奇的语法糖了…

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK