1

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

Ray
  • 3,018
  • 8
  • 50
  • 91
  • 5
    We need to see the code you actually have in order to determine exactly what's wrong. – BoltClock Nov 16 '11 at 18:41
  • 1
    From what you write you should pass NULL comma separated values. $obj = new Whatever($parm1,$parm2,$parm3,NULL,NULL,NULL); – Phill Pafford Nov 16 '11 at 18:43
  • You need to put something there. NULL for example or 0. It's not allowed to skip parameter values: new MyClass($param1,$param2,$param3,NULL,NULL,$param6); is a correct form. – ioseb Nov 16 '11 at 18:45

3 Answers3

5

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);     
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
  • Hi Here's the code [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] But when I try and create the object I used Null as @loseb suggests and got it to initiate but because I used Null it appears to overwrite the default values as Null So, if that's the case presumably I need to test for Null on the passed parameters and then set the default variables within the object? ANy help appreciated while I learn – Ray Nov 16 '11 at 20:33
  • Thanks for the update, I think I understand now - thank you. so, per comments below, do you use construct with parameters or do you use setters - or a combination. What's best practice? Thanks all! – Ray Nov 16 '11 at 23:07
  • Thats a loaded question as it depends on what you're trying to accomplish. I would suggest reading up on Design Patterns to better grasp the subject. Also know how the object/data is used would also contribute to my answer. As someone who is beginning OOP a recommend read would be: http://berryllium.nl/2011/02/getters-and-setters-evil-or-necessary-evil/ – Phill Pafford Nov 17 '11 at 00:04
0

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.

Len
  • 542
  • 1
  • 5
  • 11
0

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.

Mike Purcell
  • 19,847
  • 10
  • 52
  • 89
  • Thank you for the tip. Funny bit I did do this on the first attempt at creating the class but then picked up another article that used the __construct parameters. I'll have to have a think now!!! Thanks again – Ray Nov 16 '11 at 22:59