PracticeClasses, objects, methods and properties

Practice: Classes, objects, methods and properties

In the "Classes, objects, methods and properties" tutorial we learned about the most basic components in the field including properties that are the class's variables, methods which are functions inside a class, and objects which are created out of classes.

In this practice section, we can improve our understanding of the material by answering multiple choice questions as well as by writing our own classes and objects with the help of some guiding questions.

Coding exercise

Almost every application or blog handles users. Whether it's the registration process, the login and logout, sending reminders to users who lost their passwords, or changing the passwords on demand, all of the code that handles users should be grouped into one user class. In our example, we call the class that handles users User, in agreement with the prevailing naming convention.

Let's write a user class with the tools we have just acquired. This class will contain the first and last name of each user and will be able to say hello to anyone who uses our application.

Write what you think should be the class name, the names of the properties for the first and last name, and the name of the method that returns hello.

class name:

class properties: ,

class method:

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

 

Write the class User and add the properties:

That's how we start to write a class:

class User {
  // Your code here
}
Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

 

Add the method that says hello:

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

 

Create the first instance, and call it $user1. Use the new keyword to create an object from the class.

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

 

Set the values for the first and last name to $user1.

$firstName = 'John'
$lastName = 'Doe'

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

 

Get the user first and last name, and print it to the screen with echo.

Expected result:
John Doe

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

 

Use the hello() method with the first and last name variables in order to say hello to the user:

Expected result:
hello, John Doe

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code

 

Add another object, call it $user2, give it a first name of 'Jane' and last name of 'Doe', then say hello to the user.

Expected result:
hello, John Doe
hello, Jane Doe

Solution:

Scratchpad to practice your coding *This will not be saved nor submitted to us.*

<?php
//Your practice code
comments powered by Disqus