使用的php内部函数,PHP一些用起来很方便的内部函数

extract(array[,extract_rules,prefix]) 将数据的键值赋给以键名为变量的变量

$a = "Original";

$my_array = array("a" => "Cat","b" => "Dog", "c" => "Horse");

extract($my_array);

echo "\$a = $a; \$b = $b; \$c = $c";

// 输出 $a = Cat; $b = Dog; $c = Horse

0

1

2

3

4

5

$a="Original";

$my_array=array("a"=>"Cat","b"=>"Dog","c"=>"Horse");

extract($my_array);

echo"\$a = $a; \$b = $b; \$c = $c";

// 输出 $a = Cat; $b = Dog; $c = Horse

method_exists()检查一个对象里的某方法是否存在。

如果有,就返回TRUE,如果没有,就返回FALSE,这里并没有考虑可见性的问题。所以,当你恰好判断一个私有或者受保护的方法时,你能够得到一个正确的返回,但是执行的时候,会得到一个“Fatal Error”错误警告。

if (method_exists($object, 'SomeMethod')) {

$object->SomeMethod($this, TRUE);

}

0

1

2

3

if(method_exists($object,'SomeMethod')){

$object->SomeMethod($this,TRUE);

}

is_callable() 函数接收一个回调参数,可以指定一个函数名称或者一个包含方法名和对象的数组,如果在当前作用域中可以执行,就返回TRUE。

// is_callable()和method_exists()的区别

class Foo {

public function PublicMethod(){}

private function PrivateMethod(){}

public static function PublicStaticMethod(){}

private static function PrivateStaticMethod(){}

}

$foo = new Foo();

$callbacks = array(

array($foo, 'PublicMethod'),

array($foo, 'PrivateMethod'),

array($foo, 'PublicStaticMethod'),

array($foo, 'PrivateStaticMethod'),

array('Foo', 'PublicMethod'),

array('Foo', 'PrivateMethod'),

array('Foo', 'PublicStaticMethod'),

array('Foo', 'PrivateStaticMethod'),

);

foreach ($callbacks as $callback){

var_dump($callback);

var_dump(method_exists($callback[0], $callback[1]));

var_dump(is_callable($callback));

echo str_repeat('-', 10);

echo '
';

}

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

// is_callable()和method_exists()的区别

classFoo{

publicfunctionPublicMethod(){}

privatefunctionPrivateMethod(){}

publicstaticfunctionPublicStaticMethod(){}

privatestaticfunctionPrivateStaticMethod(){}

}

$foo=newFoo();

$callbacks=array(

array($foo,'PublicMethod'),

array($foo,'PrivateMethod'),

array($foo,'PublicStaticMethod'),

array($foo,'PrivateStaticMethod'),

array('Foo','PublicMethod'),

array('Foo','PrivateMethod'),

array('Foo','PublicStaticMethod'),

array('Foo','PrivateStaticMethod'),

);

foreach($callbacksas$callback){

var_dump($callback);

var_dump(method_exists($callback[0],$callback[1]));

var_dump(is_callable($callback));

echostr_repeat('-',10);

echo'
';

}

property_exists() 检查对象或类是否具有该属性。

说明:

bool property_exists ( mixed $class , string $property )

class 字符串形式的类名或要检查的类的一个对象

property 属性的名字

class myClass {

public $mine;

private $xpto;

static protected $test;

static function test() {

var_dump(property_exists('myClass', 'xpto')); //true

}

}

var_dump(property_exists('myClass', 'mine')); //true

var_dump(property_exists(new myClass, 'mine')); //true

var_dump(property_exists('myClass', 'xpto')); //true, as of PHP 5.3.0

var_dump(property_exists('myClass', 'bar')); //false

var_dump(property_exists('myClass', 'test')); //true, as of PHP 5.3.0

myClass::test();

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

classmyClass{

public$mine;

private$xpto;

staticprotected$test;

staticfunctiontest(){

var_dump(property_exists('myClass','xpto'));//true

}

}

var_dump(property_exists('myClass','mine'));//true

var_dump(property_exists(newmyClass,'mine'));//true

var_dump(property_exists('myClass','xpto'));//true, as of PHP 5.3.0

var_dump(property_exists('myClass','bar'));//false

var_dump(property_exists('myClass','test'));//true, as of PHP 5.3.0

myClass::test();

get_defined_vars() 此函数返回一个包含所有已定义变量列表的多维数组,这些变量包括环境变量、服务器变量和用户定义的变量。

个人感觉这个在调试或者修改未接触过的源码时很有用。

$b = array(1,1,2,3,5,8);

$arr = get_defined_vars();

// 打印 $b

print_r($arr["b"]);

// 打印所有服务器变量

print_r($arr["_SERVER"]);

// 打印变量数组的所有可用键值

print_r(array_keys(get_defined_vars()));

0

1

2

3

4

5

6

7

$b=array(1,1,2,3,5,8);

$arr=get_defined_vars();

// 打印 $b

print_r($arr["b"]);

// 打印所有服务器变量

print_r($arr["_SERVER"]);

// 打印变量数组的所有可用键值

print_r(array_keys(get_defined_vars()));

func_get_args() 此函数返回一个包含函数参数列表的数组。

array func_get_args ( void )

获取函数参数列表的数组。

该函数可以配合 func_get_arg() 和 func_num_args() 一起使用,从而使得用户自定义函数可以接受自定义个数的参数列表。

function foo()

{

$numargs = func_num_args();

echo "Number of arguments: $numargs
\n";

if ($numargs >= 2) {

echo "Second argument is: " . func_get_arg(1) . "
\n";

}

$arg_list = func_get_args();

for ($i = 0; $i < $numargs; $i++) {

echo "Argument $i is: " . $arg_list[$i] . "
\n";

}

}

foo(1, 2, 3);

?>

0

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

functionfoo()

{

$numargs=func_num_args();

echo"Number of arguments: $numargs
\n";

if($numargs>=2){

echo"Second argument is: ".func_get_arg(1)."
\n";

}

$arg_list=func_get_args();

for($i=0;$i

echo"Argument $i is: ".$arg_list[$i]."
\n";

}

}

foo(1,2,3);

?>

以上例程会输出:

Number of arguments: 3

Second argument is: 2

Argument 0 is: 1

Argument 1 is: 2

Argument 2 is: 3

0

1

2

3

4

Numberofarguments:3

Secondargumentis:2

Argument0is:1

Argument1is:2

Argument2is:3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值