0% found this document useful (0 votes)
191 views6 pages

OOP in PHP - Learn by Doing-1

This document discusses object-oriented programming (OOP) concepts in PHP, including: 1. How to declare a class with the 'class' keyword and define properties and methods within curly braces. 2. How to create objects from a class using the 'new' keyword and access object properties using '->'. 3. The use of the '$this' keyword to access a class's own properties and methods from within the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
191 views6 pages

OOP in PHP - Learn by Doing-1

This document discusses object-oriented programming (OOP) concepts in PHP, including: 1. How to declare a class with the 'class' keyword and define properties and methods within curly braces. 2. How to create objects from a class using the 'new' keyword and access object properties using '->'. 3. The use of the '$this' keyword to access a class's own properties and methods from within the class.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Title: OOP in PHP - Get your hands dirty with code 

How to create classes?


● We declare the class with the class keyword.
● We write the name of the class and capitalize the first letter.
● If the class name contains more than one word, we capitalize each word. This is known as upper camel
case. For example, JapaneseCars, AmericanIdol, EuropeTour, etc.
● We circle the class body within curly braces. We put our code within the curly braces.

<?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.

$bmw = new Car ();

We can create more than one object from the same class.

$bmw = new Car ();


$mercedes = new Car ();

How to get an object’s properties?


● In order to get a property, we write the object name, and then dash greater than (->),
and then the property name.
● Note that the property name does not start with the $ sign; only the object name starts
with a $.

echo $bmw -> color;


echo $mercedes -> color;

Write the output here -


A)___________
B)___________
How to set an object property?
in order to set the color to ‘blue’ in the bmw object

$bmw -> color = 'blue';


$bmw -> comp = "BMW";
$mercedes -> comp = "Mercedes Benz";

How to add methods to a class?


● We put the public keyword in front of a method.
● The naming convention is to start the function 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, helloUser() or flyPanAm().
class Car {
public $comp;
public $color = "beige";
public $hasSunRoof = true;
public function hello() {
return "beep";
}
}

Write down the output of the codes below


$car1 = new Car ();
$car2 = new Car ();
echo $car1 -> hello();
echo $car2 -> hello();

A)_____________________
B)______________________

QUIZ

1.1 Which of these definitions best explains the term ‘class’?


A: A collection of variables and functions working with these variables.
B: Sets and gets the class’s own properties and methods.
C: Is the embodiment of a real life object.
1.2 Which of these definitions best explains the term ‘object’?
A:An object gives us the ability to work with the class, and to have several instances of the same class.
B: A variable within a class.
C: A function within a class.
1.3 Which of these definitions best explains the term ‘property’?
A: Groups the code that is related to one topic.
B: A variable within a class.
C: A function within a class.
1.4 Which of these definitions best explains the term ‘method’?
A: A function within a class.
B: A variable within a class.
C: The embodiment of a real action.

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.

Concept: The $this keyword

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();?>

Write Down the output of the above code


A)
echo $mercedes -> hello();
Write Down the output of the above code
A)

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

Access modifiers: public vs private

The public access modifier

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();

What is the output ?


A)

The private access modifier

<?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();

What is the output ?


A)

How to access a private property?

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();

What is the output now?


A)
QUIZ:
4.1 We use the private access modifier in order to:
A : Limit the access to a class.
B : Limit the access to properties.
C : Limit the access to methods.
D : B+C

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.

You might also like