Jump to content

Recommended Posts

I was reading a tutorial about OOP, and there is something makes me confused. Here is the code :

<?php
class person { 
    public $name;
    public $age; 

    public function __construct() 
       { 
        $this->name = "mr smith";
        $this->age = 0;
       }
    public function blabla etc..  
}
?>

I know what the constructor is for.. But we could code this class like this :

<?php
class person { 
    public $name = "mr smith";
    public $age = 0; 
    
    public function blabla.. etc.
}
?>

So my question is :why he assigned the variables like in the first example, we can code it like the second example though. What's the benefit of the first one ?

Link to comment
https://forums.phpfreaks.com/topic/174140-why-to-assign-into-constructor/
Share on other sites

<?php
class person { 
    public $name;
    public $age; 

    public function __construct($n, $g) 
       { 
        $this->name = $n;
        $this->age = $g;
       }
}

$a = new person("my name" , "my age");

?>

 

it would nicer to work this way, right?

for your example, yeah it doesn't really matter. However, that is a tutorial, and it really is just showing you the syntax of how constructors go, and not really trying to convince you that you should always assign variables in your constructor.

 

by the way, you can also make a constructor this way

class foo{

//below is the constructor
public function foo($msg){
echo $msg;
}
}

 

now if you were to create the function like so

 

$foo = new foo("Hello World!");

 

it would output

Hello World!

 

kind of off topic, but yeah

a constructor is just the function to trigger when the object is instantiated.. whether you specify it in the class or in a method inside the class, it will still do the same thing, however, there is an exception to this.. if you make the variable declared within the class not within the method static than you can access it like this:

 

foo::$variable;

 

but that is mostly for outside use as its more strict with static values.

it depends on your situation. if you want the constructor to assign variables that you define, IE

class foo{
var $value;
public function foo($value){
$this->value = $value;
}
}

However, if whenever you instantiate the class, the variable will always have the same value, then assigning it in the declaration part and the constructor basically do the same thing

To the OP, imagine you're creating your Person objects from data stored in the database... how likely is it that all of those people will be named Mr. Smith?

i just wanted to show the usage of both decleration.

let me change the example ;

<?php
class brand { 
    public $name;
    public $size; 

    public function __construct() 
       { 
        $this->name = "not available";
        $this->size = 5;
       }
    public function blabla etc..  
}
?>

To the OP, imagine you're creating your Person objects from data stored in the database... how likely is it that all of those people will be named Mr. Smith?

 

everybody with a black suit! if this was the matrix that is.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.