简体   繁体   English

如果未设置,则传递默认参数

[英]Passing default parameters if not set

I am trying to pass a variable if it hasn't been set. 我正在尝试传递一个尚未设置的变量。 Here is the old method: 这是旧的方法:

function CHECK_DATE($DATE, $FORMAT='Y-m-d', $var3) {
CHECK_DATE(0000-00-00 00:00:00, '', '');

This is how I would have done it in procedural style. 这就是我本应以程序样式完成的方式。 Therefore, if the second parameter is empty then it would fall back to 'Ym-d'. 因此,如果第二个参数为空,则它将退回到'Ym-d'。

I have the following (which doesn't work unless I remove all empty parameters): 我有以下内容(除非删除所有空参数,否则它将不起作用):

class Date {
public $date;
public $format;

public function setDate($date, $format='Y-m-d') {
    $this->date = date($format, strtotime($date));  
}

public function getDate() {
    return $this->date;
    echo 'test';        
}
}

    $getDate = new Date;
    $getDate->setDate('2013-08-31 13:05:18', '');   
    echo $getDate->getDate();

You can define it the same in your class definition as in your procedural example, like so: 您可以在类定义中定义与过程示例中相同的定义,如下所示:

public function setDate($date, $var3, $format='Y-m-d') {
        $this->date = date($format, strtotime($date));          
    }

EDIT: After doing testing of my own, here's what I can verify works: 编辑:经过我自己的测试,这是我可以验证的工作:

<?php
class Date {
public $date;
public $format;

public function setDate($date, $format='Y-m-d') {
        $this->date = date($format, strtotime($date)); 

    }

public function getDate() {
    echo $this->date;        
}
}

    $getDate = new Date;
    $getDate->setDate('2013-08-31 13:05:18');   
    echo $getDate->getDate();
?>

The problem is when you were doing $getDate->setDate , doing 问题是当您执行$ getDate-> setDate时,

$getDate->setDate('2013-08-31 13:05:18', ''); 

sets the format to '' 将格式设置为“

The following tutorial might be of some help. 以下教程可能会有所帮助。

Basically, make sure that all your optional arguments are at the far right side of the function's argument list. 基本上,确保所有可选参数都在函数的参数列表的最右边。 You may define an argument as optional by providing a value for it while declaring the function. 您可以通过在声明函数时为其提供值来将参数定义为可选参数。

So in your example the function may be defined as follows: 因此,在您的示例中,该函数可以定义如下:

public function setDate($date, $var3, $format='Y-m-d') {
        $this->date = date($format, strtotime($date));          
}

Now if you call it with just 2 arguments (skipping the third) it will be assumed to be 'Ym-d'. 现在,如果仅使用2个参数(跳过第三个参数)来调用它,则将其假定为'Ym-d'。 Eg 例如

$data->setDate('2013-08-31 13:05:18', $myVar);

However, the following will fail, since it's missing the second parameter, which is not defined as optional (doesn't have a default value): 但是,以下操作将失败,因为它缺少第二个参数,该参数未定义为可选参数(没有默认值):

$data->setDate('2013-08-31 13:05:18');

Note that you may define a default value to be NULL or FALSE as well. 请注意,您也可以将默认值定义为NULL或FALSE。

http://php.net/manual/en/functions.arguments.php http://php.net/manual/zh/functions.arguments.php

I am not sure how you got your function to work as expected because the PHP.net docs state: 我不确定您如何使函数按预期工作,因为PHP.net文档指出:

Note that when using default arguments, any defaults should be on the right side of any non-default arguments; 请注意,使用默认参数时,任何默认值都应位于所有非默认参数的右边; otherwise, things will not work as expected. 否则,事情将无法按预期进行。

So you would have to have been checking the value of the second variable in your procedural style for things to have worked correctly. 因此,您必须一直在检查过程样式中的第二个变量的值,以确保一切正常。

Your first example you are passing empty strings, not null values, so the default values are being overwritten. 第一个示例传递的是空字符串,而不是null值,因此默认值将被覆盖。

personally I'd just do: 我个人只是这样做:

class Date {

    public $date = null;

    public function setDate($date=null, $dateFormat='Y-m-d', $var3=null) {
        $this->date = date($dateFormat, strtotime($date));          
    }

    public function getDate() {
        return $this->date;
    }
}


$getDate = new Date;
$getDate->setDate('2013-08-31 13:05:18'); // Y-m-d
$getDate->setDate('2013-08-31 13:05:18', null, null);   // Y-m-d
$getDate->setDate('2013-08-31 13:05:18', 'Y-d-m', null);  // Y-d-m

no need for a public $format unless you plan on using it elsewhere. 除非您计划在其他地方使用它,否则不需要公共的$ format。 In which case you could pass it as in the constructor and set it there, or use another method. 在这种情况下,您可以像在构造函数中那样传递它并在那里进行设置,或者使用其他方法。

If you want to use the public value, then this is one approach: 如果要使用公共价值,那么这是一种方法:

class Date {
    public $date = null; // or protected, or private
    public $dateFormat = 'Y-m-d'; // or protected, or private

    public function setDate($date) {
        $this->date = date($this->dateFormat, strtotime($date));
    }

    public function setDateFormat($dateFormat) {
        $this->dateFormat = $dateFormat;
    }

    public function getDate() {
        return $this->date;
    }
}

$date = new Date();
$date->setDateFormat('Y-d-M');
$date->setDate('2013-08-31 13:05:18');
echo $date->getDate();

This will do what you want: 这将做您想要的:

<?php
class Date {
    public $date;
    public $format;

    public function setDate($date, $var3, $format="Y-m-d") {
        $this->date = date($format, strtotime($date));   
    }

    public function getDate() {
        return $this->date;
    }
}
$getDate = new Date;
$getDate->setDate('2013-08-31 13:05:18', '');   
echo $getDate->getDate();
?>

Your problem is that you are setting an unsetting $format. 您的问题是您要设置一个未设置的$ format。 First (when the class was called) public $format = 'Ym-d'; 首先(当该类被调用时) public $format = 'Ym-d'; then when you called set date you overwrote it to '' . 然后当你调用的设置日期,你可以覆盖它来'' That broke your date function. 那破坏了您的date功能。 Defaulting $format in public function setDate will only work if you don't overwrite that default during your call (ie: setDate('2013-08-31 13:05:18', '', '') ). public function setDate默认$format仅在您在调用过程中不覆盖默认值的情况下才起作用(即: setDate('2013-08-31 13:05:18', '', '') :05: setDate('2013-08-31 13:05:18', '', '') )。 (Meaning my solution is not the only solution, but you have to pick one and stick with it!) Hope this helps. (意味着我的解决方案不是唯一的解决方案,但是您必须选择一个并坚持下去!)希望这会有所帮助。

setDate($ date,$ format = $ this-> format,$ var3)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM