can you explain this to me

Associate
Joined
19 Jul 2006
Posts
1,847
OK so I have this
PHP:
<?php
	class person{
		var $name;
        
        function __construct($persons_name)
        {
            $this->name =$persons_name;
        }
		function set_name($new_name){
			$this->name = $new_name;
		}
		function get_name(){
			return $this->name;
		}
	}

if I do this
PHP:
$dave = new person();
    $matthew = new person('Matthew Smith');

I get warnings and notices about the missing argument and undeclared variable for $dave as I expected. I understand all of that.
To fix it I have seen
PHP:
 function __construct($persons_name = NULL)
        {
            $this->name =$persons_name;
        }

Which works and gets rid of the errors. can you explain to me why though.

Why does $sam = new person('sam'); get the name set to sam? I know we are sending it into the constructor as $persons_name but we are also setting $persons_name to NULL in the constructor.

The code works I just want to understand the function __construct($persons_name = NULL) bit
 
Associate
Joined
10 Aug 2011
Posts
138
don't know php really but looks like you are passing a default value of null to $persons_name

This means that when you don't pass a value in, the initializer function (__construct) still has something to use when it calls $this->name = $persons_name, and if you pass a value in it'll just use the value you passed instead of the default
 
Associate
OP
Joined
19 Jul 2006
Posts
1,847
Yeah that's what's happening
its just in my head when you assign a variable eg
$x = 'name' then from there on in $x == name until you reassign it.

so my thinking ( which is wrong )
PHP:
 function __construct($persons_name = NULL)

we have set $persons_name to be null then any reference to it after that should be null.
 

Pho

Pho

Soldato
Joined
18 Oct 2002
Posts
9,325
Location
Derbyshire
don't know php really but looks like you are passing a default value of null to $persons_name

This means that when you don't pass a value in, the initializer function (__construct) still has something to use when it calls $this->name = $persons_name, and if you pass a value in it'll just use the value you passed instead of the default



^ this. You're saying $persons_name = the name I pass into the constructor OR null if I haven't passed one in.
 
Associate
Joined
10 Aug 2011
Posts
138
Yeah that's what's happening
its just in my head when you assign a variable eg
$x = 'name' then from there on in $x == name until you reassign it.

so my thinking ( which is wrong )
PHP:
 function __construct($persons_name = NULL)

we have set $persons_name to be null then any reference to it after that should be null.

The value null only comes into effect when the function doesn't receive the parameter. What you're saying is normally true except that functions are not part of the main block of a program. They have special rules surrounding them. So this is just a sort of syntactic sugar that a lot of modern languages have.
 
Associate
Joined
10 Aug 2011
Posts
138
No problem. You'd end up learning about things like tokenisation/lexing/parsing/interpreting/compiling if you were to try to understand what's going on under the hood, I guess. Not necessary for almost anyone, other than simply to sate one's curiosity.

So yeah, I guess just accept that that's a special rule you can use in the parameter definition to make life easier. It's just how you define optional parameters.
 
Back
Top Bottom