您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息

如何学习方法参数类型声明

2024/5/2 10:46:17发布37次查看
本篇文章给大家介绍一下学习方法参数类型声明的方法。有一定的参考价值,有需要的朋友可以参考一下,希望对大家有所帮助。
不管从事什么行业,现在都是活到老学到老的趋势,特别是我们这堆码农。这回也不用说新技术用不上,光光是php文档的学习都会发现非常多的知识点其实自己并没有真正的掌握,比如说这个方法参数的类型声明。
上次文章中,关于php的方法参数类型约束,我们说过方法参数的类型约束仅限于类、接口、数组或者callable回调函数,其实这是不严谨的,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 stringteststring('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 接口。
总结就像开头说过的,原来在严格模式下我们的语法还会有这么大的差异,这回真的是长见识了。我们的学习之路还很长,也希望各位能够持续关注一起加油!!
测试代码:
https://github.com/zhangyue0503/dev-blog/blob/master/php/202003/source/%e5%86%8d%e6%ac%a1%e5%ad%a6%e4%b9%a0%e6%96%b9%e6%b3%95%e5%8f%82%e6%95%b0%e7%b1%bb%e5%9e%8b%e5%a3%b0%e6%98%8e.php
推荐学习:php视频教程
以上就是如何学习方法参数类型声明的详细内容。
该用户其它信息

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录 Product