PHP 盲点扫描 01

31 Dec 2015 Category: PHP

PHP语言由于其语法简单,没有数据类型,因此会有不少不易被发现的盲点,这些盲点一旦在 应用中出现,非常具有隐蔽性,很难发现,因为很多都会让你觉得不科学。但是,当你 仔细阅读PHP手册,还真是这样。

$a = null;
var_dump($a['tttt']);//输出null,不报错。

$b = array();
var_dump($b['tttt']);//throw exception undefiend index
$x = 0123;          // 八进制,相当于十进制83
$y = "0123" + 0     // 十进制123
<?php
namespace test\example;
class Obj {
    function test() {
        echo __FILE__ . "<br/>"; //绝对路径
        echo __LINE__ . "<br/>"; //行号
        echo __NAMESPACE__ . "<br/>"; //完整命名空间test\example
        echo __FUNCTION__ . "<br/>"; //当前函数名称test
        echo __METHOD__ . "<br/>"; //带命名空间的类方法test\example\Obj::test
        echo __CLASS__ . "<br/>"; //带命名空间的类test\example\Obj
    }
}
$obj = new Obj();
$obj::test();
?>

<?php

class base_class
{
    function say_a()
    {
        echo "'a' - said the " . __CLASS__ . "<br/>";
    }

    function say_b()
    {
        echo "'b' - said the " . get_class($this) . "<br/>";
    }

}

class derived_class extends base_class
{
    function say_a()
    {
        parent::say_a();
        echo "'a' - said the " . __CLASS__ . "<br/>";
    }

    function say_b()
    {
        parent::say_b();
        echo "'b' - said the " . get_class($this) . "<br/>";
    }
}

$obj_b = new derived_class();

$obj_b->say_a();
echo "<br/>";
$obj_b->say_b();

?>

输出结果:

'a' - said the base_class
'a' - said the derived_class

'b' - said the derived_class
'b' - said the derived_class

__CLASS__获取的是__CLASS__所在的类的类名 getclass($this)是获取当前类的类名.

<?php

class MyParent {
    public $name = 'parent';
    function test() {
        var_dump($this);
        echo $this->name . "<br/>";
    }
}

class Child extends MyParent {
    public $name = 'child';
    function test() {
        parent::test();
        echo $this->name . "<br/>";
    }
}

$parent = new MyParent();
$parent->test();

$child = new Child();
$child->test();
?>

输出结果

object(MyParent)#1 (1) { ["name"]=> string(6) "parent" } parent
object(Child)#2 (1) { ["name"]=> string(5) "child" } child
child
$a = "012";
$b = "AA";

for($i=0;$i<5;$i++)
{
    echo ++$a;//输出13,14,15,16,17
    echo ++$b;//输出AB,AC,AD,AE,AF
}

评论