1. php自定义函数时怎么指定参数类型
class User{
public $name;
public $password;
function __construct($name,$password){
$this->name=$name;
$this->password=$password;
}
}
//参数可以指定对象类型
function f1(User $user){
echo $user->name,”,”,$user->password;
}
//参数可以指定数组类型
function f2(array $arr){}
//参数不可以指定基本类型,下面一句会出错
function f3(string $s){}
2. php函数设定参数类型
php 函数的参数类型可以指定为类名或数组类型array,比如
这样是对的public function Right( My_Class $a, array $b )
这样是错的public function Wrong( string $a, boolean $b )
如果需要其他类型,需要在函数内部进行类型检查
参考
http://www.php.net/manual/zh/functions.arguments.php
这一段
public function Right( My_Class $a, array $b )
tells first argument have to by object of My_Class, second an array. My_Class means that you can pass also object of class that either extends My_Class or implements (if My_Class is abstract class) My_Class. If you need exactly My_Class you need to either make it final, or add some code to check what $a really.
Also note, that (unfortunately) "array" is the only built-in type you can use in signature. Any other types i.e.:
public function Wrong( string $a, boolean $b )
will cause an error, because PHP will complain that $a is not an *object* of class string (and $b is not an object of class boolean).
So if you need to know if $a is a string or $b bool, you need to write some code in your function body and i.e. throw exception if you detect type mismatch (or you can try to cast if it's doable).
3. php 如何将指定变量类型转换成时间格式
$newDateStr = date('Y-m-d',strtotime('20161220'));
4. PHP的对象方法声明中指定形参类型是什么意思
function function_name( $a){
$b=$a*2;
return $b;
}
比如这个function ,$a 你就要给他指定类型为 整形或浮点型
5. php如何判断某变量的类型
1、gettype()
gettype 会根据 参数类型返回值 。
例如:
gettype('1');返回的是string。
而gettype(1);返回的是integer。
2、empty
如果 变量 是非空或非零的值,则 empty() 返回 FALSE。换句话说,”"、0、”0″、NULL、FALSE、array()、var $var、未定义;以及没有任何属性的对象都将被认为是空的,如果 var 为空,则返回 TRUE。
3、isset
如果 变量 存在(非NULL)则返回 TRUE,否则返回 FALSE(包括未定义)。变量值设置为:null,返回也是false;unset一个变量后,变量被取消了。注意,isset对于NULL值变量,特殊处理。
(5)php指定类型扩展阅读
PHP 在变量定义中不需要(或不支持)明确的类型定义;变量类型是根据使用该变量的上下文所决定的。也就是说,如果把一个 string 值赋给变量$var,$var就成了一个 string。如果又把一个integer 赋给$var,那它就成了一个integer。
PHP 的自动类型转换的一个例子是乘法运算符“*”。如果任何一个操作数是float,则所有的操作数都被当成float,结果也是float。否则操作数会被解释为integer,结果也是integer。注意这并没有改变这些操作数本身的类型;改变的仅是这些操作数如何被求值以及表达式本身的类型。
6. php怎么删除某一目录下的指定文件类型
php中删除文件有一个系统函数:
unlink ( string $filename );
参数$filename 表示文件的路径,可以是相对路径也可以是绝对路径。
列如,当前目录下有个文件:test.html
可以执行 unlink ( 'test.html' );来删除
另外删除目录用函数:rmdir();用法与unlink ()相同
你可以去后盾人平台看看,里面的视频对你是很有帮助的
7. 怎么才能让PHP文本框中输入指定类型的值
11用jquery ui对input做验证,规定输入类型。222用正则,input有个属性pattern赋值正则,可以。
8. PHP在定义变量时,是否需要明确指定变量的类型
完全不需要,PHP是弱类型语言。
9. 如何指定 PHP 数据类型
php 函数的参数类型可以指定为类名或数组类型array,比如 这样是对的public function Right( My_Class $a, array $b ) 这样是错的public function Wrong( string $a, boolean $b ) 如果需要其他类型
10. 在PHP方法(或成员函数)上除了对象可作为参数限定类型外,还有什么可作为参数限定类
如果你指的是在定义php函数(方法)时,对参数类型进行类型限定的话(类似C和DELPHI语言的强类型检测定义的那种),那么,据查阅资料,答案如下:
php只有数组和对象两种限定类型。
PHP 5 可以使用类型约束。函数的参数可以指定只能为对象(在函数原型里面指定类的名字),php 5.1 之后也可以指定只能为数组。
注意,即使使用了类型约束,如果使用NULL作为参数的默认值,那么在调用函数的时候依然可以使用NULL作为实参。