This is something I have never been fully sure of or never found a solid answer for.
Lets say I have a User class with a register() method inside it and I’m not sure which way is best to implement this method.
In my register.php page should I have
$user->register($_POST['firstName'], $_POST['lastName'], $_POST['username'], etc..);
and then in the register() method don’t bother setting the objects attributes and just use the variables supplied in the signature of the method or should I do
$user->register();
and then in the register function do something like
$this->firstName = $_POST['firstName'];
$this->lastName = $_POST['lastName'];
etc...
Thanks in advance.
Considering
$_POSTis defined in the global scope, it would make more sense to use your latter approach (not passing in arguments and setting it up from the function). NOTE however, that this will only work in the case that$_POSTis declared in the global scope (in this case) and you will lose flexibility in scenarios when you pass in the class from external PHP modules.