English 中文(简体)
PHP 类型回归法
原标题:PHP class method return type

是否有可能确定这种返回类型?

public static function bool Test($value)
{
      return $value; //this value will be bool
}
最佳回答

As per this page : http://php.net/manual/en/language.types.type-juggling.php

你可以尝试这样做。

return (boolean) $value;

现在,PHP提供PHP的返回类型申报 7. 基本生活 我已解释过how to use Return category

问题回答

www.un.org/Depts/DGACM/index_spanish.htm 由于这个问题仍在搜索引擎成果中,这里是一个最新的答案:。

PHP 7 actually introduced proper return types for functions / methods as per this RFC.

The example from the Manual associated above:

function sum($a, $b): float {
    return $a + $b;
}

Or, in a more general notation:

function function_name(): return_type {
    // some code
    return $var // Has to be of type `return_type`
}

如果返回的变数或价值与返回类型不符,PHP将默示将其转换为这种类型。 或者,你可以通过<条码>对文档的限定打字<>/a>(限值-型号=1);,在这种情况下,一种不匹配会导致。 页: 1

简便。 然而,铭记你们需要确保PHP 7在你的开发和生产服务器上都能够提供。

不可能在方法/功能层面明确确定返回类型。 如前所述,你可以投递回,例如......。

return (bool)$value;

或者,你可以在phpDoc syntax中添加一个意见,许多民主选举学会将从某种完成的角度来看待这一类型。

/**
 * example of basic @return usage
 * @return myObject
 */
function fred()
{
    return new myObject();
}

PHP 7.0 您可以宣布返回类型如下:

function returnABool(): bool {
    return false;
}

不仅常见数据类型,你也可以退回预期的物体。 例如,你期望一项职能永远返回某类。 这一点可以实现:

class Biography {
    public function name() {
        return  John Doe ;
    }
}

现在你可以宣布此类物体的返回类型:

function get_bio(): Biography {
    return new Biography();
}

当功能或方法回收出乎意料的数据类型时,有人投掷了TypeError。 这方面的一个例子是,你将碰到<>TypeError。

class Biography {
    public function name() {
        return  John Doe ;
    }
}

class Bio2 {
    public function name() {
        return  John Doe ;
    }
}

function get_bio(): Biography {
    return new Bio2;
}

不能放弃任何<>的榜样。 类型:

class Biography {
    public function name() {
        return  John Doe ;
    }
}

class Bio2 {
    public function name() {
        return new Biography();
    }
}

function get_bio(): Biography {
    $bio2 = new Bio2;
    return $bio2->name();
}

关于申报职务和方法的返回数据类型的更多详情,可从以下网站查阅:

是的,你可以将其投到<代码>bool。

public static function something($value) {
    return (bool)$value;
}

Although from PHP 7, you can explicitly force a return type:

public static function something($value) : bool {
    return $value;
}

如果你知道返还值的类型是ool,那么该功能中就没有点。

There is no way to define the return type of a function within PHP as it is loosely-typed. You will have to do your validation within the calling function.

Not at the moment in PHP 5 or below, you can define type in function sign but not in the return value. Strict declarations are in mind on PHP ver. 6 and above, but are not implemented yet.

Maybe with some tricks you can do this, like casting before returning the value...

你们可以做的另一项工作是,确保压倒一切的人不会改变返回的类型。

public function Test($value)
{
      return (bool) OnTest();
}

private function OnTest($value)
{
      return $value;
}

通过这样做,你可以允许从贵阶层继承的班子改变《全面禁止核试验条约》的功能,但仍然确保测试回合价值。

而你可以这样做:

public static function Test($value)
{
      return (bool) $value; //this value will be bool
}

据我所知,你可以使用一个接口,强迫返回类型。





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签