Part 1  Grammer

尽管PHP的语法已经很松散,写起来很“爽”。但是对于学过 Java 的“完全面向对象程序员“来说,PHP程序设计语言里,还是有一些的坑的。下面请让我来盘点一下。

Parse error: syntax error, unexpected T_STRING, expecting T_FUNCTION in......

  错误原因:在PHP语法中,声明任何函数,函数名的前面需要 function 关键字。

<?php
//错误代码如下
class Test{
__construct(){
echo 'I am construction!';
}
}

  正确示例:每一次声明函数(方法),都要写上“function”这个关键字。无论您想声明的是 __construct()这类魔术方法,还是自定义函数,都逃不出 function 的手掌心。

<?php
class Test{
  //正确代码如下
function __construct(){
echo 'I am construction!';
}
}

PHP Fatal error:  Class Service_Page_Statistic_ZhiziBase contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (Service_Page_Statistic_ZhiziBase::executeHook) in /home/map/odp_wmaudit/app/wmaudit/models/service/page/statistic/ZhiziBase.php on line 18

Fatal error: Access to undeclared static property: ......

  错误原因:self::只能指向静态属性,而指向非静态属性只能用 $this->。

<?php
//错误代码
class Person{
function __construct($name){
self::$name = $name;
}
private $name;
} $paul = new Person('paul');

  正确方法:self::指向静态属性,$this->指向非静态属性。

<?php
//正确代码 $this
class Person{
function __construct($name){
$this->$name = $name;
}
private $name;
} $paul = new Person('paul');
<?php
//正确代码 self::
class Person{
function setBirthday($date){
self::$birthday = $date;
}
static private $birthday;
} $paul = new Person();
$paul->setBirthday('1990-01-01');

Fatal error: Cannot redeclare A::__construct() in......

  错误原因:PHP不支持函数重载

  解决方法:使用PHP内置函数 func_num_args() 、func_get_arg() 、func_get_args()来模拟实现OOP的函数重载

<?php
class Test{
function __construct(){
switch(func_num_args()){
case 0:
echo 'no argument';
default:
echo 'the number of arguments is '.func_num_args(). '<br />';
$argumentArray = func_get_args();
//遍历方法一
foreach($argumentArray as $key => $value){
echo 'the No'. $key. ' argument is '. $value. '<br />';
}
echo '<br />';
//遍历方法二
for($i=0; $i<func_num_args(); $i++){
echo 'the No'. $i. ' argument is '. func_get_arg($i). '<br />';
}
}
echo '<hr />';
}
} new Test();
new Test(1);
new Test(1,2,3);

Warning: file_get_contents(0): failed to open stream: No such file or directory in

  错误原因:字符串相加结果“意外”等于“0”。

  解决方法1:“点”操作符(.)连接字符串。

<?php

$url = "http://op.juhe.cn/onebox/train/query?";
$param = "name=Tank&age=22"; $data = file_get_contents( $url. $param );

  解决方法2:使用PHP内置函数 implode() 将数组连接成字符串。  

<?php

$url = "http://op.juhe.cn/onebox/train/query?";
$param = "name=Tank&age=22"; $url = implode('', array($url, $param));
$data = file_get_contents( $url );

PHP Fatal error:  Class ...... contains 1 abstract method and must therefore be declared abstract or implement the remaining methods ([类名]::[方法名]) in /home/....../xxx.php on line 18

  错误原因:该类也需要是抽象类。如果一个普通类包含了抽象方法,那么该类被实现了怎么办?被实现的时候,类中的抽象方法还没有被定义,这一定是不安全的。另外,PHP 中的抽象方法都是完全抽象的。

  解决方法:使声明了抽象方法的类都定义为抽象类。

Part 2  语言本身

变量的引用

PHP 引用有毒。PHP 的引用和 C++ 类似,但是不完全一样,使用不当会引入bug。

foreach($data as &$val) {
&val = 1;
}

数组插入顺序

PHP 数组是基于 HashTable 实现的,但不同于 C++ STL 的 std::map(如下定义)。PHP 的 key 是不排序的,其遍历顺序会遵从“插入时的顺序”。

template<
class Key,
class T,
class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T> >
> class map;

下面这段关于 std::map keys 排序的介绍,很好地诠释了std::map 是如何运作的。以辅助理解 PHP 的数组。

std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare.

细数php里的那些“坑”的更多相关文章

  1. 细数Qt开发的各种坑(欢迎围观)

    1:Qt的版本多到你数都数不清,多到你开始怀疑人生.从4.6开始到5.8,从MSVC编译器到MINGW编译器,从32位到64位,从Windows到Linux到MAC.MSVC版本还必须安装对应的VS2 ...

  2. 细数JDK里的设计模式

    原文出处: javacodegeeks   译文出处:deepinmind 这也是篇老文了,相信很多人也看过.前面那些废话就不翻译了,直接切入正题吧~ 结构型模式: 适配器模式: 用来把一个接口转化成 ...

  3. 细数JDK里的设计模式<转>

    这也是篇老文了,相信很多人也看过.前面那些废话就不翻译了,直接切入正题吧~ 结构型模式: 适配器模式: 用来把一个接口转化成另一个接口. java.util.Arrays#asList() javax ...

  4. 细数Python Flask微信公众号开发中遇到的那些坑

    最近两三个月的时间,断断续续边学边做完成了一个微信公众号页面的开发工作.这是一个快递系统,主要功能有用户管理.寄收件地址管理.用户下单,订单管理,订单查询及一些宣传页面等.本文主要细数下开发过程中遇到 ...

  5. 细数 Windows Phone 灭亡的七宗罪(过程很详细,评论很精彩,但主要还是因为太慢了,生态跟不上,太贪了,厂商不愿意推广)

    曾梦想仗剑走天涯,看一看世界的繁华 年少的心有些轻狂,如今你四海为家 曾让你心疼的姑娘,如今已悄然无踪影 犹记得上大学攒钱买了第一台智能手机Lumia 520时,下载的第一首歌曲<曾经的你> ...

  6. 细数iOS上的那些安全防护

    细数iOS上的那些安全防护  龙磊,黑雪,蒸米 @阿里巴巴移动安全 0x00 序 随着苹果对iOS系统多年的研发,iOS上的安全防护机制也是越来越多,越来越复杂.这对于刚接触iOS安全的研究人员来说非 ...

  7. 细数AutoLayout以来UIView和UIViewController新增的相关API

    本文转载至 http://www.itjhwd.com/autolayout-uiview-uiviewcontroller-api/ 细数AutoLayout以来UIView和UIViewContr ...

  8. 细数.NET 中那些ORM框架 —— 谈谈这些天的收获之一

    细数.NET 中那些ORM框架 —— 谈谈这些天的收获之一(转) ADO.NET Entity Framework        ADO.NET Entity Framework 是微软以 ADO.N ...

  9. 迄今最安全的MySQL?细数5.7那些惊艳与鸡肋的新特性(上)【转载】

    转自: DBAplus社群 http://www.toutiao.com/m5762164771/ 迄今最安全的MySQL?细数5.7那些惊艳与鸡肋的新特性(上) - 今日头条(TouTiao.com ...

随机推荐

  1. VS中去掉空格虚点

    Ctrl + R+W 可以在VS中添加或移除 空格虚点.

  2. ReentrantLock源码(二)

    一.ReentrantLock类中的方法解读. 1.lock方法.实现了接口Lock中的lock方法.这里实际上是调用了sync成员变量的lock方法来实现.所以取决于sync的实现. 2.unloc ...

  3. 学习笔记<2>Android基本四大组件

    <1>Activity(活动) ------与用户交互的界面 (1)Activity启动基本流程 <2>service(服务) <3>ContentProvider ...

  4. meta 跳转

    强无敌. 是否厌倦了页面枯燥的跳转? 试试这样的跳转吧. Duration:表示滤镜特效的持续时间(单位:秒) Transition:滤镜类型.表示使用哪种特效,取值为0-23. <Meta h ...

  5. Orangegreenworks封装rpgmakermv

    You’ll get a zip file with a folder called “lib” and a file called greenworks.js. Put both of them o ...

  6. css解决无论页面长短footer永远置底

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  7. C-Cow Sorting (置换群, 数学)

    Farmer John's N (1 ≤ N ≤ 10,000) cows are lined up to be milked in the evening. Each cow has a uniqu ...

  8. Lua 判断表是否为空方法

    [1]判断表为空的方法 目前为止,Lua语言中判断table表是否为空有三种方式: (1)#table,当table为数组时直接返回table表的长度. (2)当table是字典时,返回table的长 ...

  9. linux 虚拟机挂载光驱

    step 1 step 2 step 3 挂载 root@vm-xiluhua /dev # mount cdrom /mnt mount: /dev/sr0 写保护,将以只读方式挂载 step 4 ...

  10. linux常用命令:cd 命令

    Linux cd 命令可以说是Linux中最基本的命令语句,其他的命令语句要进行操作,都是建立在使用 cd 命令上的.所以,学习Linux 常用命令,首先就要学好 cd 命令的使用方法技巧. 1. 命 ...