php之函数

scope(空间)
unpack (解压)
Traversable (穿越)
performance(性能)
experiment (检验)
properties (属性)

trailing (尾随)

function(函数)

用户自定义函数

一个函数可由以下的语法来定义:

任何有效的PHP代码都有可能出现在函数内部,甚至包括其他函数和类定义。

函数名和PHP中的其他标识符命名规则相同。

函数无需再调用之前被定义,除非是函数是有条件被定义时。

当一个函数是有条件被定义时,必须在调用函数之前定义。

PHP中的所有函数和类都具有全局作用域,可以定义在一个函数之内而在之外调用,反之亦然。

PHP不支持函数重载,也不可能取消定义或者重定义已声明的函数。

Note:函数名是大小写无关的,不过在调用函数的时候,使用其在定义时相同的形式是个好习惯。

PHP的函数支持可变数量的参数和默认参数。参见func_num_args(),func_get_arg()和func_get_args()。

在PHP中可以调用递归函数。

Note:但是哟啊避免递归函数/方法调用超过100-200层,因为可能会使堆栈崩溃从而使当前脚本终止。无线递归可视为编程错误。

函数的参数

通过参数列表可以传递信息到函数,即可以逗号作为分隔符的表达式列表。参数是从左向右求值的。

PHP支持按值传递参数(默认)。通过引用传递参数以及默认参数。也支持可变长度参数列表。

通过引用传递参数以及默认参数。也支持可变长度参数列表。

默认情况下,函数参数通过值传递(因而即使在函数内部改变参数的值,它并不会改变函数外部的值)。如果希望
允许函数修改它的参数值,必须通过引用传递参数。

如果想要函数的一个参数总是通过引用传递,可以在函数定义中该参数的前面加个符号&:

默认参数的值

函数可以定义C++风格的标量参数默认值,

PHP还允许使用数组array和特殊类型null作为默认参数,

默认值必须是常量表达式,不能是诸如变量,类成员,或者函数调用等。

注意当时用默认参数时,任何默认参数必须方在任何非默认参数的右侧;否侧,
函数将不会按照预期的情况工作。

Note:自PHP5起,传引用的参数也可以有默认值。

类型声明:

Note:在PHP5中,类型声明也被称为类型提示。

类型声明允许函数在调用时要求参数为特定类型。如果给出的值类型不对,那么将会产生一个错误:在PHP5中,
这将是一个可恢复的致命错误,而在PHP7中将会抛出一个TypeError异常。

为了制定一个类型声明,类型应该加到参数名前。这个声明可以通过将参数的默认值设为null来实现允许传递null。

valid types

type description php version

Class/interface naem the parameter must be an instanceof the given
class or interface name. php5.0.0

self the parameter must be an instanceof the same class
as the one the method is defined on. this can only be
used on class and interface methods. php5.0.0

array The parameter must be an array php5.1.0

callable the parameter must be a valid callable php5.4.0
bool the parameter must be a boolean value. php 7.0.0
float the parameter must be a floating point value php7.0.0
int The parameter must be an integer php7.0.0
string the parameter must be a string php7.0.0

Warnming Aliases for the above scalar types are not supported。Instead,they are treated as class or
interface names,for example,using Boolean as a parameter or return type will require an argument or
return value thant is an instanceof the class or interface boolean,rather than of type bool:

严格类型:

默认情况下,如果能做到的话,PHP将会强迫错误类型的值转化为函数期望的标量类型。
例如,一个函数的一个参数期望是string,但传入的是int,最终函数得到的将会一个string类型的值。

可以基于每个文件开启严格模式。在严格模式下,只有一个与类型声明完全相符的变量才会被接受,否则将会抛出
一个TypeError。唯一的一个例外是可以将int传给一个期望float的函数。

使用declare语句和strict_types声明来启用严格模式:

Caution(警告):启用严格模式同时也会影响返回值类型声明。

Note:
严格类型适用于在启用严格模式的文件内的函数调用,而不是在那个文件内声明的函数。一个没有启用
严格模式的文件内调用了一个在启用严格模式的文件中的定义的函数,那么将会遵循调用的偏好(弱类型),
而这个值将会被转换。

严格模式一定在函数被调用的地方声明。

Note:
严格类型仅用于标量类型声明,也正式因为如此,这需要PHP7.0.0或者更新版本,因为标量类型声明也是在哪个版本中添加的。

可变数量的参数列表:

PHP在用户自定义函数中支持可变数量的参数列表。在PHP5.6及以上的版本中,由...语法实现;
在PHP5.5及更早版本中,使用函数func_num_args(),func_get_arg(),和func_get_args()。

... in php 5.6+

in php5.6 an later,argument lists may include the ... token to denote that the function accepts a
variable number of arguments. the arguments will be passed into the given variable as an array;for example:

Using ... to access variable arguments

You can also use ... when calling functions to unpack an array or Traversable variable or literal into the argument lists;

Using ... to provide arguments

You may specify normal positional argumens before the ... token。In this case,only the trailing argumens that
don't match a positional argumen will be added to the array generated by ...

it is also possible to add a type hint before the ... token, if this is present,then all arguments captured by ... must
be objects of the hinted class.

①:Type hinted variable arguments

Older versions of PHP

No specia syntax is required to note that a function is variadic;however access to the function's arguments must
use func_num_args(),func_get_arg()和func_get_args()。

The first example above would be implemented as follows in php5.5 and earlier;

User Contributed Notes :

To experiment on performance of pass-by-reference and pass-by-value, I used this scipt。

Conclusions(结论) are below。

Conclusions:

1、PHP is already smart about zero-copy / copy-on-write。A function call does NOT copy the data
unless it needs to;the data is

A function's argument that is an object, will have its properties modified by the function although you don't
need to pass it by reference。

This is different for arrays:

You can use a class constant as default parameter。

You can pass a function as an argument too.

If you use ... in a function's parameter list, you can use it only once for obvious reasons.

Less obvious is that it has to be on the LAST parameter; as the manual puts it:

You may specify normal positional arguments before the ... token

返回值:

值通过使用可选的返回语句返回。可以返回包括数组和对象的任意类型。
返回语句会立即中职函数的运行,并且将控制权回调该函数的代码行。

Note:
如果省略了return,则返回值为null。

return的使用

函数不能返回多个值,但可以通过返回一个数组来得到类型的效果。

//返回一个数组以得到多个返回值。

//从函数返回一个引用,必须在函数声明和指派返回值给一个变量时都使用引用运算符&:

//可变函数
php支持可变函数的概念。这意味着如果一个变量名后有圆括号,PHP将寻找与变量的值同名的函数,
并且尝试执行它。可变函数可以用来实现包括回调函数,函数表在内的一些用途。

可变函数不能用户例如echo,print,unset(),isset(),empty(),include,require以及类似的语言结构。
需要使用自己的包装函数来将这些结构用作可变函数。

也可以用可变函数的语法来调用一个对象的方法。

当调用静态方法时,函数调用要比静态属性优先:

匿名函数(Anonymous functions),也叫闭包函数(closures),允许 临时创建一个没有指定名称的函数。最经常用作回调函数(callback)参数的值。当然,也有其它应用的情况

匿名函数目前是通过 Closure 类来实现的

闭包可以从父作用域中继承变量。 任何此类变量都应该用 use 语言结构传递进去。 PHP 7.1 起,不能传入此类变量: superglobals、 $this 或者和参数重名。

//Inherited variable's value is from when the function is defined,
//not when called

这些变量都必须在函数或类的头部声明。 从父作用域中继承变量与使用全局变量是不同的。
全局变量存在于一个全局的范围,无论当前在执行的是哪个函数。
而 闭包的父作用域是定义该闭包的函数(不一定是调用它的函数)。示例如下:

as of 7.0,you can use IIFR by wrapping your anonymous function with ()

转载于:https://www.cnblogs.com/2018-05-9-ygk/p/9479753.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值