📖 Scenario: You are creating a simple PHP class to represent a Book in a library system. Each book has a title and an author. You want to control how these properties can be accessed and changed from outside the class.
🎯 Goal: Build a PHP class called Book with properties that have different visibility levels. Learn how to set and get these properties safely.
📋 What You'll Learn
Create a class named Book
Add a public property called title
Add a private property called author
Add a public method setAuthor to set the author property
Add a public method getAuthor to get the author property
Create an instance of Book and set the title and author
Print the title and author of the book
💡 Why This Matters
🌍 Real World
Controlling access to data inside objects is important in real-world software to protect sensitive information and keep code organized.
💼 Career
Understanding properties and visibility is a key skill for PHP developers working on web applications, APIs, and backend systems.
Progress0 / 4 steps
1
Create the Book class with a public title property
Create a class called Book with a public property named title and set it to an empty string.
PHP
Hint
Use public string $title = ""; inside the class to create the property.
2
Add a private author property
Inside the Book class, add a private property called author and set it to an empty string.
PHP
Hint
Use private string $author = ""; to keep the author hidden from outside.
3
Add public setter and getter methods for author
Add two public methods inside Book: setAuthor(string $name) to set the author property, and getAuthor() to return the author property.
PHP
Hint
Use $this->author = $name; inside setAuthor and return $this->author; inside getAuthor.
4
Create a Book instance, set properties, and print them
Create a new Book object called $myBook. Set its title to "1984" and use setAuthor to set the author to "George Orwell". Then print the title and author in the format: Title: 1984, Author: George Orwell.
PHP
Hint
Use $myBook->title = "1984"; and $myBook->setAuthor("George Orwell");. Then print with print("Title: {$myBook->title}, Author: {$myBook->getAuthor()}");.
Practice
(1/5)
1. Which visibility keyword allows a property to be accessed from anywhere, including outside the class?
easy
A. private
B. public
C. protected
D. static
Solution
Step 1: Understand visibility keywords
Public properties can be accessed from anywhere, protected only within the class and subclasses, private only inside the class.
Step 2: Identify which keyword allows outside access
Only public allows access from outside the class.
Final Answer:
public -> Option B
Quick Check:
public = accessible everywhere [OK]
Hint: Public means accessible everywhere, no restrictions [OK]
Common Mistakes:
Confusing protected with public
Thinking private allows outside access
Mixing static with visibility
2. Which of the following is the correct way to declare a private property named $name in a PHP class?
easy
A. private $name;
B. var $name;
C. public $name;
D. protected $name;
Solution
Step 1: Recall PHP property declaration syntax
Properties are declared with visibility keyword followed by variable name and semicolon.
Step 2: Identify private property syntax
Correct syntax is private $name;. The var keyword is outdated and defaults to public.
Hint: Use 'private' keyword before $property for private visibility [OK]
Common Mistakes:
Using 'var' instead of visibility keywords
Omitting the $ sign
Using public or protected instead of private
3. What will be the output of this PHP code?
class Car {
private $color = 'red';
public function getColor() {
return $this->color;
}
}
$car = new Car();
echo $car->getColor();
medium
A. Empty string
B. Error: Cannot access private property
C. null
D. red
Solution
Step 1: Understand private property access
The property $color is private, so it cannot be accessed directly outside the class.
Step 2: Check method usage to access private property
The public method getColor() returns the private property value, so calling it outputs 'red'.
Final Answer:
red -> Option D
Quick Check:
Private property accessed via public method = 'red' [OK]
Hint: Use public methods to access private properties safely [OK]
Common Mistakes:
Trying to access private property directly
Expecting an error when using a getter method
Confusing null with private property value
4. Identify the error in this PHP class code:
class Person {
protected $age;
public function setAge($age) {
$this->age = $age;
}
}
$person = new Person();
echo $person->age;
medium
A. Method setAge should be private
B. Missing semicolon after property declaration
C. Cannot access protected property directly outside class
D. Class name should be lowercase
Solution
Step 1: Understand protected property visibility
Protected properties cannot be accessed directly from outside the class or its subclasses.
Step 2: Analyze code usage of protected property
The code tries to echo $person->age directly, which causes an error due to protected visibility.
Final Answer:
Cannot access protected property directly outside class -> Option C
Quick Check:
Protected = no direct outside access [OK]
Hint: Protected properties need methods for outside access [OK]
Common Mistakes:
Trying to access protected property directly
Confusing protected with public
Ignoring visibility rules
5. Given this PHP class, what is the best way to safely update the private property $score only if the new score is higher?
class Game {
private $score = 0;
public function updateScore(int $newScore) {
// What code should go here?
}
public function getScore() {
return $this->score;
}
}
hard
A. if ($newScore > $this->score) { $this->score = $newScore; }
B. $this->score = $newScore;
C. return $newScore > $this->score;
D. if ($newScore < $this->score) { $this->score = $newScore; }
Solution
Step 1: Understand the requirement to update only if new score is higher
The property $score should change only when $newScore is greater than current $score.
Step 2: Choose the correct conditional update code
if ($newScore > $this->score) { $this->score = $newScore; } checks if $newScore is greater, then updates $score. Other options either overwrite unconditionally or do wrong comparison.
Final Answer:
if ($newScore > $this->score) { $this->score = $newScore; } -> Option A
Quick Check:
Update only if newScore > current score [OK]
Hint: Use if condition to update private property safely [OK]
Common Mistakes:
Updating score without condition
Using wrong comparison operator
Returning boolean instead of updating property
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.