Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

Chapter 2 PHP Fundamentals

$user = new UserClass(); does exactly what it shows: the variable


$user is now going to have the configuration and format that you
described in the class UserClass. This also demonstrates the importance
of proper naming in PHP. With one look at this line of code, you can have
a high percentage of certainty that this variable $user is most likely going
to be associated with $UserClass somehow and less likely to be associated
with a class named $DumpTruckClass.
The next two lines call a method (function) you create within
UserClass. These specific types of methods, Get____ and Set___, are
known as helpers and also commonly referred to as the class “getters” and
“setters.” These methods simplify the task of setting values within the object.
The last line is a PHP-specific function used heavily in debugging
code. var_dump() shows you exactly what is in a variable and what type
of variable you are analyzing. In your example, calling var_dump shows all
the information within the object. $user is the bag of Skittles and $user-
>firstName is the individual Skittle.
Now, how do you use this all together? You need to specify the class
information in one file and include it in another file. Thankfully, you
already know how to do that. Let’s open up main3.php.

<code>
<?php
$title = "main3 php file";
require "UserClass.php";
$user = new UserClass;
$user->setFirstName = 'gunnard';
$user->setLastName = 'engebreth';
var_dump($user);
error_reporting( E_ALL );
ini_set( "display_errors",1);
$html =include_once "inc/template2.php";
</code>

24
Chapter 2 PHP Fundamentals

You have already reviewed what the first two lines do, so set up and set
the variable $user as a part of the class UserClass. The next two lines set
the first name and last name. If you run this script on the command line or
browser, you can verify this through the var_dump() function. Figure 2-2
shows what you should see.

Figure 2-2. Code result web page

This is a fundamental stage in development. You have created an


object, assigned values, and can display the information on the page. Now
you need to make it dynamic by adding user interaction via forms.
Forms are more than just what you use to comment on someone’s
picture on Facebook. Forms are the method by which a user can interact
with a program. The user can directly communicate with the code and the
database you have created. With this kind of access, great detail must be
put into validation and sanitization of input, which we will get to later. For
now, just get comfortable with getting and using input. Let’s get technical
about receiving data from the user.

Verbs: GET and POST


HTTP (Hyper Text Transfer Protocol) is what connects the Web that
you know today. This is the protocol or method of agreed-upon
communication that allows for servers, PHP, and users to talk to one

25
Chapter 2 PHP Fundamentals

another. There are a lot of specifications within HTTP for everything


including error handling, expected standard document formats, and
request methods. These methods are defined in five verbs:

GET

The GET method requests a representation of the


specified resource. Requests using GET should only
retrieve data.

POST

The POST method is used to submit an entity to the


specified resource, often causing a change in state or
side effects on the server.

PUT

The PUT method replaces all current representations


of the target resource with the request payload.

DELETE

The DELETE method deletes the specified resource.

PATCH
The PATCH method is used to apply partial
modifications to a resource.

You will be focusing on GET and POST but rest assured, you will be using
the others as you build your REST API.
Point your browser to http://localhost:8000/chapter2/main4.
php?pants=123. Notice in the URL you have main4.php?pants=123. When
the page loads, it should look like Figure 2-3.

26
Chapter 2 PHP Fundamentals

Figure 2-3. URL result web page

At the top is var_dump() and you can see that the info you have in
the URL is now available to you in PHP as the variable $userVars. This is
available to you through the HTTP verb GET and in PHP you use the global
variable $_GET. GET specifically allows for the transfer of data through the
URL. You can send multiple values as well. Change the URL to include

<code>
http://localhost:8000/chapter2/main4.php?pants=123&dog=poodle&f
ood=spaghetti
</code>

Refresh the page and you will now see that Pants, Dog, and Food have
values set to them. The other method of transmitting data from the user to
your code is using POST.
The POST verb behaves in nearly the same way but does not use the
URL, thereby keeping the data you are transmitting a bit more secure. In
order to see the POST functionality, open main5.php and take a look.

<code>
<?php
$userVars = $_POST;
$title = "main5 php file";
require "UserClass.php";
$user = new UserClass;
$user->setFirstName = 'gunnard';
$user->setLastName = 'engebreth';
27

You might also like