资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

php的方法参数类型有哪些

本篇内容介绍了“php的方法参数类型有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

站在用户的角度思考问题,与客户深入沟通,找到雨花台网站设计与雨花台网站推广的解决方案,凭借多年的经验,让设计与互联网技术结合,创造个性化、用户体验好的作品,建站类型包括:网站建设、成都网站设计、企业官网、英文网站、手机端网站、网站推广、域名与空间、雅安服务器托管、企业邮箱。业务覆盖雨花台地区。

严格模式的定义:

declare (strict_types = 1);

int 类型

function testInt(int $a)
{
    echo $a, PHP_EOL;
}

testInt(1);
// testInt(1.1); // Fatal error: Uncaught TypeError: Argument 1 passed to testInt() must be of the type int
// testInt('52AABB'); // Fatal error: Uncaught TypeError: Argument 1 passed to testInt() must be of the type int
// testInt(true); // Fatal error: Uncaught TypeError: Argument 1 passed to testInt() must be of the type int

在严格模式下,很明显地看出现在这个方法的参数只能接收 int 类型的值了,其他的类型都无法接收,当然也不会像之前文章说过的那样会发生强制转换。

float 类型

function testFloat(float $a)
{
    echo $a, PHP_EOL;
}

testFloat(1);
testFloat(1.1);
// testFloat('52AABB'); // Fatal error: Uncaught TypeError: Argument 1 passed to testInt() must be of the type int
// testInt(true); // Fatal error: Uncaught TypeError: Argument 1 passed to testInt() must be of the type int

这里需要注意的是,PHP只有 int 和 float,而且 float 是 int 的超集,所以这里是可以传整数过来的,不过上面的 testInt(int $a) 则不能接收 1.1 这样的 float 值。这就涉及到了上下转换的问题,向超集转换是OK的,但是超集向子集转换是就不OK了。

string 类型

function testString(string $a)
{
    echo $a, PHP_EOL;
}

// testString(1);  // Fatal error: Uncaught TypeError: Argument 1 passed to testString() must be of the type string
// testString(1.1);  // Fatal error: Uncaught TypeError: Argument 1 passed to testString() must be of the type string
testString('52AABB');
// testString(true); // Fatal error: Uncaught TypeError: Argument 1 passed to testString() must be of the type string

这个就不用过多解释了,在非严格模式下我们如果定义 string 类型的接收参数的话,其实是任何类型都可以接收过来做为 string 类型的,这里的类型转换就不多说了,可以说在非严格模式下定义 string 类型的效果跟没有任何定义是一样的。但是严格模式下就不同了,真的是只能接收双引或者单引号之内的字符串内容。

bool 类型

function testBool(bool $a)
{
    var_dump($a);
}
testBool(true);
testBool(false);
// testBool('52AABB'); // Fatal error: Uncaught TypeError: Argument 1 passed to testBool() must be of the type bool
// testBool(1); // Fatal error: Uncaught TypeError: Argument 1 passed to testBool() must be of the type bool

布尔值也是同理的,这里我们也只能接收 true 和 false 关键字的值。

新学习一个 iterable 类型

最后来介绍个新家伙,除了普通模式下的类、数组、回调函数,严格模式下的各种标量类型声明外,还有一个 iterable 类型的声明,相信大家通过这个单词也能看出来了,可迭代的类型。

function testIterable(iterable $iterator)
{
    echo gettype($iterator), ':', PHP_EOL;
    foreach ($iterator as $it) {
        echo $it, PHP_EOL;
    }
}

testIterable([1, 2, 3]);
testIterable(new ArrayIterator([1, 2, 3]));
// Generator对象
testIterable((function () {
    yield 1;
    yield 2;
    yield 3;
})());
// testIterable(1); // Fatal error: Uncaught TypeError: Argument 1 passed to testIterable() must be iterable

没错,它包含了数组、实现迭代器接口的类以及生成器相关的内容。也就是所有可用 foreach 迭代的内容都可以传递过来。生成器本身会是一个 Generator 对象,而在学习PHP生成器的使用这篇文章中,我们已经看过这个 Generator 对象的内容,它本身也是实现了 Iterator 接口。

“php的方法参数类型有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注创新互联网站,小编将为大家输出更多高质量的实用文章!


当前标题:php的方法参数类型有哪些
链接地址:http://cdkjz.cn/article/pdjjis.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220