Bird
Raised Fist0
PHPprogramming~20 mins

Properties and visibility in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
PHP Properties Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
What is the output of this PHP code with private property?
Consider this PHP class with a private property. What will be the output when trying to access the private property directly from outside the class?
PHP
<?php
class Box {
    private $content = 'secret';
}
$box = new Box();
echo $box->content;
?>
AFatal error: Uncaught Error: Cannot access private property Box::$content
Bnull
Csecret
DWarning: Undefined property: Box::$content
Attempts:
2 left
💡 Hint
Private properties cannot be accessed directly outside the class.
❓ Predict Output
intermediate
2:00remaining
What will this PHP code output when accessing a protected property from a subclass?
Look at this PHP code where a subclass tries to access a protected property from its parent. What is the output?
PHP
<?php
class Animal {
    protected $type = 'mammal';
}
class Dog extends Animal {
    public function getType() {
        return $this->type;
    }
}
$dog = new Dog();
echo $dog->getType();
?>
AWarning: Undefined property: Dog::$type
BFatal error: Cannot access protected property Animal::$type
Cnull
Dmammal
Attempts:
2 left
💡 Hint
Protected properties are accessible in subclasses.
🔧 Debug
advanced
2:00remaining
Why does this PHP code cause an error when accessing a static private property?
This PHP code tries to access a private static property from a subclass. What error occurs and why?
PHP
<?php
class ParentClass {
    private static $count = 0;
    public static function increment() {
        self::$count++;
    }
    public static function getCount() {
        return self::$count;
    }
}
class ChildClass extends ParentClass {
    public static function increment() {
        self::$count++;
    }
}
ChildClass::increment();
echo ChildClass::getCount();
?>
AFatal error: Uncaught Error: Access to undeclared static property ChildClass::$count
B1
C0
DWarning: Undefined property: ChildClass::$count
Attempts:
2 left
💡 Hint
Private static properties are not accessible in subclasses.
📝 Syntax
advanced
2:00remaining
Which option correctly declares a public property with a default value in PHP 8.1+?
Choose the correct syntax to declare a public property with a default value in a PHP class using typed properties.
Apublic $name: string = 'John';
Bpublic string $name = 'John';
Cpublic string $name: 'John';
Dpublic $name = string 'John';
Attempts:
2 left
💡 Hint
Typed properties use type before the variable name with an equals sign for default.
🚀 Application
expert
2:00remaining
How many properties are accessible from outside the class in this PHP code?
Given this PHP class with different visibility properties, how many properties can be accessed directly from an instance outside the class?
PHP
<?php
class Car {
    public $make = 'Toyota';
    protected $model = 'Corolla';
    private $year = 2020;
}
$car = new Car();
// How many properties can be accessed directly like $car->property ?
?>
A0
B2
C1
D3
Attempts:
2 left
💡 Hint
Only public properties are accessible from outside the class.

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

  1. Step 1: Understand visibility keywords

    Public properties can be accessed from anywhere, protected only within the class and subclasses, private only inside the class.
  2. Step 2: Identify which keyword allows outside access

    Only public allows access from outside the class.
  3. Final Answer:

    public -> Option B
  4. 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

  1. Step 1: Recall PHP property declaration syntax

    Properties are declared with visibility keyword followed by variable name and semicolon.
  2. Step 2: Identify private property syntax

    Correct syntax is private $name;. The var keyword is outdated and defaults to public.
  3. Final Answer:

    private $name; -> Option A
  4. Quick Check:

    private property declaration uses 'private' keyword [OK]
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

  1. Step 1: Understand private property access

    The property $color is private, so it cannot be accessed directly outside the class.
  2. Step 2: Check method usage to access private property

    The public method getColor() returns the private property value, so calling it outputs 'red'.
  3. Final Answer:

    red -> Option D
  4. 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

  1. Step 1: Understand protected property visibility

    Protected properties cannot be accessed directly from outside the class or its subclasses.
  2. Step 2: Analyze code usage of protected property

    The code tries to echo $person->age directly, which causes an error due to protected visibility.
  3. Final Answer:

    Cannot access protected property directly outside class -> Option C
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    if ($newScore > $this->score) { $this->score = $newScore; } -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard