Frage

I'm learning php oop, going through books etc and having a go at my own.

In my __construct I have a number of parameters that have default values (in fact all of them do). However when I try to write code to create a new object I'm having syntax error problems.

For example my construct statement has 6 parameters. I passed values to the first three, wanted to skip the next two and set a value for the last one. I simply put commas with nothing between them but it throws up a syntax error.

What should I put in place of nothing to accept the default value?

Thanks

War es hilfreich?

Lösung

Pass NULL instead of nothing

Example:

$obj = new Whatever($parm1,$parm2,$parm3,NULL,NULL,NULL); 

or in your constructor set the to null

__construct($parm1 = NULL,$parm2 = NULL,$parm3 = NULL,$parm4 = NULL,$parm5 = NULL,$parm6 = NULL)

And call it like this

 $obj = new Whatever();   

UPDATE from your comment:

Your code:

function __construct(
    $userid, 
    $magicCookie, 
    $accessLvl = 'public', 
    $det       = 'basic', 
    $sortOrder = 'starttime', 
    $recurring = 'true', 
    $d_from    = "01/01/2011", 
    $d_to      = "31/01/2011", 
    $max       = "10") {
    // Code goes here...
} 

So calling like this

// After the seconds parameter you have default values
$obj = new Whatever(
    $userid, 
    $magicCookie
    );  

What do you think about this?

function __construct(
    $userid, 
    $magicCookie, 
    $accessLvl = NULL, 
    $det       = NULL, 
    $sortOrder = NULL, 
    $recurring = FALSE, 
    $d_from    = NULL, 
    $d_to      = NULL, 
    $max       = NULL) {
    // Code goes here... Yes set Defaults if NULL
}

Call it like this:

$obj = new Whatever(
    $userid, 
    $magicCookie, 
    $accessLvl = 'public', 
    $det       = 'basic', 
    $sortOrder = 'starttime', 
    $recurring = TRUE, 
    $d_from    = "01/01/2011", 
    $d_to      = "31/01/2011", 
    $max       = 10);     

Andere Tipps

Try passing empty quotes. You may have to use logic in your constructor to check for blanks and set a value. The ternary operator ()?: is a good choice.

The solution posted by Phil is probably what you are after, in that you are not respecting each parameter of the constructor even though there may not be a value.

In that you are learning OOP, I'd like to offer you an alternative solution. Over the years I have come to find that passing params to a constructor ends up in situations where the arg list becomes unwieldy. Consider this example (Demonstrative purposes only):

class Car
{
    public function __construct($color = NULL, $transmission = NULL, $bodyStyle = NULL, $interior = NULL, $engineType = NULL, $wheelSize = NULL, $radioType = NULL) {
        // Do stuff
    }
}

With a long list of args passed to arg, EVERY object I instantiate I have to check to make sure I am passing values in the correct order, which becomes tedious if I haven't seen the code in awhile.

// Example 1
$myCar = new Car('blue', 'automatic', 'sedan', 'leather', 'v6', '18', 'xmBluetooth');

// Example 2
$myCar = new Car('green', 'manual', NULL, NULL, 'v8', NULL);

// Example 3
$myCar = new Car(NULL, NULL, 'sedan', NULL, NULL, NULL, NULL, 'cdPlayer');

What I would recommend doing is using setters to set these values, then every time the object is instantiated, only the necessary values need to be set, without having to deal with order of entry in a long argument list.

class Car {
    protected $color;
    protected $bodyStyle;
    protected $transmission;
    protected $interior;
    protected $engineType;
    protected $wheelSize;
    protected $radioType;

    public function setColor($color) 
    {
        $this->color = $color;
    }

    public function setTransmission($transmission)
    {
        $this->transmission = $transmission;
    }


    // etc.
}

// Calling code
$myCar = new Car();
$myCar->setColor('green');
$myCar->setWheelSize('18');
$myCar->setInterior('leather');
$myCar->setEngineType('v6');

$yourCar = new Car();
$yourCar->setColor('black');

Just something to think about.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top