I'm a new to coding, and I'm having some trouble understanding a script that sets the new object's properties. Please help me. Thank you in advance.
First, in sign_up.php, I have a input element with POST method as such...
<?php include_once('classes/signup.class.php'); ?>
...
<input type="text" class="input-xlarge" id="name" name="name"
value="<?php echo $signUp- >getPost('name'); ?>" placeholder="<?php _e('Full name'); ?>">
In the signup.class.php, I have the following code...
class SignUp
function __construct() {
if(!empty($_POST)) {
foreach ($_POST as $field => $value)
$this->settings[$field] = $value;
$this->process();}
public function getPost($var) {
return empty($this->settings[$var]) ? '' : $this->settings[$var];}
}
$signUp = new SignUp();
To my understanding, the getPost($var) will return empty until the form is submitted. Once it is submitted, __constructor() will execute the foreach loop and for the second time, $value will call to parse getPost('name'). This time around, what is $this->settings[$var]?
I am confused, and any of your help will be much appreciated.
Here's your code from classes/signup.class.php
(with indentation):
class SignUp {
function __construct() {
if (!empty($_POST)) {
foreach ($_POST as $field => $value) {
$this->settings[$field] = $value;
}
$this->process();
}
}
public function getPost($var) {
return empty($this->settings[$var]) ? '' : $this->settings[$var];
}
}
$signUp = new SignUp();
And here's what happens step by step:
The code is being evaluated from top to bottom.
The first thing that the interpreter evaluates is the definition of your class. So, after encountering class SignUp { ... }
, the PHP interpreter will be aware that there is a class called SignUp
but it will not execute any part from it yet.
Then it encounters the $signUp = new SignUp();
instruction. At this point it instantiates an object of SignUp
class and assigns it to the $signUp
variable.
__construct()
is now being evaluated:
4.1 As $_POST
is not empty it will execute the foreach
loop.
4.2 foreach
simply iterates over each value from $_POST
assigning it to $this->settings
.
Example: assuming that $_POST = array('name' => 'John'), then upon the first iteration (and the only one), $field
will become 'name'
and $value
will become 'John'
.
4.3 Now the interpreter evaluates the $this->settings[$field] = $value;
expression which simply translates to $this->settings['name'] = 'John';
4.4 The foreach
loop ends and the $this->process()
instruction is now being evaluated.
At the end of the execution of classes/signup.class.php in your global namespace there will be an object called $signUp
which contains an array member called $settings
that consists only of a key ('name'
) being set to 'John'
Now the HTML part of your file is being interpreted and when the interpreter will reach:
value="<?php echo $signUp->getPost('name'); ?>"
it will simply evaluate the code inside the PHP tags: echo $signUp->getPost('name');
, thus calling the getPost()
method on $signUp
.
The getPost()
method simply checkes the $this->settings
member (which was previously set - when __construct
ing the object) and returns the corresponding value for key name
(which is 'John'
).