简体   繁体   中英

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'.

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('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'. 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.

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

I am not sure how you got your function to work as expected because the PHP.net docs state:

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.

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. 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. First (when the class was called) public $format = 'Ym-d'; then when you called set date you overwrote it to '' . That broke your date function. 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', '', '') ). (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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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