OOP in PHP - Learn by Doing-1
OOP in PHP - Learn by Doing-1
<?php
class Car {
// The code
}
?>
How to add properties to a class?
● We put the public keyword in front of a class property.
● The naming convention is to start the property name with a lowercase letter.
● If the name contains more than one word,all of the words,except for the first word,start with an
● uppercase letter. For example, $color or $hasSunRoof
● A property can have a default value. For example, $color = 'beige'.
● We can also create a property without a default value. See the property $comp in the below example.
class Car {
public $comp;
public $color = "beige";
public $hasSunRoof = true;
}
How to create objects from a class?
● We created the object $bmw from the class Car with the new keyword.
● The process of creating an object is also known as instantiation.
We can create more than one object from the same class.
A)_____________________
B)______________________
QUIZ
CODE Exercise
1.5 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:
(1) _____ ,
(2) _____ class method: _______
1.6 Write the class User, and add the properties. That’s how we start to write the class:
class User { // Your code here }
1.7 Add the method that says hello to the class.
1.8Create the first instance,and call it $user1.Use the new keyword to create an object from the class.
1.9 Set the values for the first and last name to $user1. $firstName = ‘John’ $lastName = ‘Doe’
1.10 Get the user first and last name, and print it to the screen with echo.
1.11Use the hello method with the first and last name variables in order to say hello to the user.
1.12 Add another object, call it $user2, give it a first name of ‘Jane’ and last name of ‘Doe’, then say hello to the user.
The $this keyword indicates that we use the class’s own methods and properties, and allows us
to have access to them within the class’s scope.
The $this keyword allows us to approach the class’s properties and methods from within the
class using the following syntax:
$this->propertyName;
$this->methodName();
● Only the this keyword starts with the $ sign, while the names of the properties and
methods do not start with it.
● The $this keyword indicates that we use the class’s own methods and properties, and
allows us to have access to them within the class’s scope.
● Let’s illustrate what we have just said on the Car class.
● We will enable the hello() method to approach the class’s own properties by using the
$this keyword.
<?php
class Car {
public $comp;
public $color = "beige";
public $hasSunRoof = true;
public function hello() {
return "Beep I am a <i>" . $this -> comp . "</i>, and I am <i>" . $this ->
color;
}
}
//Let us create two objects from the class:
$bmw = new Car();
$mercedes = new Car ();
//and set the values for the class properties:
$bmw -> comp = "BMW";
$bmw -> color = "blue";
$mercedes -> comp = "Mercedes Benz";
$mercedes -> color = "green";
// We can now call the hello method for the first car object:
echo $bmw -> hello();?>
QUIZ
2.1 Which keyword would you use in order to approach the class properties and methods from within the
class?
A. The new keyword.
B. The class keyword.
C. The $this keyword
Coding exercise:
We wrote the hello() method in side the User class.In the following exercise, we will add to the hello() method the
ability to approach the class properties with the $this keyword. First, let’s remind ourselves what the User class looks
like:
class User {
// The class properties
public $firstName;
public $lastName;
// A method that says hello to the user
public function hello()
{ return "hello"; }
}
Wouldn’t it be nicer if we could allow the hello() method the ability to get the class’s properties, so that it
would be able to say hello to the user name
(for example, “hello, John Doe”)?
1.1. Add to the hello() method the ability to approach the $firstName property, so the hello() method would be
able to return the string “hello, $firstName”.
1.2. Create a new object with the first name of ‘Jonnie’ and last name of ‘Roe’.
The $this keyword 20
1.3. Echo the hello() method for the $user1 object, and see the result
Code Exercise
class User {
// The class properties. public $firstName;
// A method that says hello to the user $firstName.
// The user $firstName property can be approached with the $this keyword.
public function hello() {
echo "hello," . $this -> firstName;
}
}
3.1 Add a register() method to the class that echoes the string “ >> registered”.
3.2 Add a mail() method to the class that echoes the string “ >> email sent”.
3.3 Add return $this to the hello() method (so it can be chained to any other method in the class).
3.4 Add return $this to the register() method (so it can also be chained.)
3.5 Create a new $user1 object with the first name of “Jane”. For this object, chain the methods in the following order:
hello() -> register() -> mail().
Expected result: hello, Jane >> registered >> email sent
class Car {
// Public methods and properties public $model;
public function getModel()
{ return "The car model is " . $this -> model; }
}
$mercedes = new Car();
// Here we access a property from outside the class
$mercedes -> model = "Mercedes";
// Here again we access another method from outside the class
echo $mercedes -> getModel();
<?php
class Car {
// Private
private $model;
public function getModel() { return "The car model is " . $this -> model; }
}
$mercedes = new Car();
// We try to access a private property from outside the class.
$mercedes -> model = "Mercedes";
echo $mercedes -> getModel();
class Car {
// The private access modifier denies access to the method // from outside the class’s
scope
private $model;
// The public access modifier allows the access to the method
// from outside the class
public function setModel($model) { $this -> model = $model; }
public function getModel() { return "The car model is " . $this -> model; }
}
$mercedes = new Car();
// Set the car’s model
$mercedes -> setModel("Mercedes");
+// Get the car’s model
echo $mercedes -> getModel();
Let’s return to the User class that we developed in the previous chapters, but let’s now define the
$firstName of the user as a private property. This is the User class:
class User {
// Your code goes here
}
4.2 Create a new class property with the name of $firstName, and prevent any code from outside the
class from changing the property value by using the appropriate access modifier (private or protected).
4.3 Create a method to set the $firstName property value.Remember to use the right access modifier
(public/private).
4.4 Now, create a method to return the $firstName value.
4.5 Create a new user object with the name of $user1, set its name to ‘Joe’ and make it return its name.