Properties and Methods in PHP Practice Questions with Solutions

Introduction

In PHP, properties and methods are important parts of a class. Properties store information about an object, such as an employee’s name or salary. Methods define what an object can do, such as displaying details or calculating a salary. In this chapter, you will practise creating and using properties and methods in PHP through practical PHP questions.

Question 1: Create a Class with Properties and a Method

Problem Statement

Create a Student class with two properties: name and course. Add a method called displayDetails() to display the student’s information.

PHP Solution

<?php

class Student
{
    public $name = "Rahul";
    public $course = "PHP";

    public function displayDetails()
    {
        echo "Student Name: " . $this->name . "<br>";
        echo "Course: " . $this->course;
    }
}

$student = new Student();

$student->displayDetails();

?>

Sample Output

Student Name: Rahul
Course: PHP

Explanation

  • $name and $course are properties of the Student class.
  • displayDetails() is a method that displays student information.
  • $this refers to the current object.
  • $this->name accesses the name property of the current object.

Concepts Covered

  • Class properties
  • Class methods
  • $this keyword
  • Object method calling

Question 2: Update Object Properties Using a Method

Problem Statement

Create an Employee class with name and salary properties. Create a method called updateDetails() to update these properties.

PHP Solution

<?php

class Employee
{
    public $name;
    public $salary;

    public function updateDetails($name, $salary)
    {
        $this->name = $name;
        $this->salary = $salary;
    }

    public function displayDetails()
    {
        echo "Employee Name: " . $this->name . "<br>";
        echo "Salary: " . $this->salary;
    }
}

$employee = new Employee();

$employee->updateDetails("Amit", 35000);

$employee->displayDetails();

?>

Sample Output

Employee Name: Amit
Salary: 35000

Explanation

  • updateDetails() accepts two parameters.
  • $this->name stores the employee’s name.
  • $this->salary stores the employee’s salary.
  • The displayDetails() method displays the updated information.

Concepts Covered

  • Updating properties through methods
  • Method parameters
  • Object property assignment

Question 3: Create Multiple Objects with Different Property Values

Problem Statement

Create a Product class with productName and price properties. Create three objects and assign different values to each object.

PHP Solution

<?php

class Product
{
    public $productName;
    public $price;

    public function displayProduct()
    {
        echo "Product: " . $this->productName . "<br>";
        echo "Price: Rs. " . $this->price . "<br><br>";
    }
}

$product1 = new Product();
$product1->productName = "Keyboard";
$product1->price = 800;

$product2 = new Product();
$product2->productName = "Mouse";
$product2->price = 500;

$product3 = new Product();
$product3->productName = "Monitor";
$product3->price = 12000;

$product1->displayProduct();
$product2->displayProduct();
$product3->displayProduct();

?>

Sample Output

Product: Keyboard
Price: Rs. 800

Product: Mouse
Price: Rs. 500

Product: Monitor
Price: Rs. 12000

Explanation

  • The Product class defines two properties and one method.
  • Three separate objects are created.
  • Each object stores its own product name and price.
  • Calling displayProduct() displays the information belonging to that particular object.

Concepts Covered

  • Multiple objects
  • Object-specific properties
  • Reusing class methods

Question 4: Calculate Total Salary Using a Method

Problem Statement

Create an Employee class with basicSalary, hra and da properties. Create a method called calculateSalary() to calculate the total salary.

PHP Solution

<?php

class Employee
{
    public $basicSalary = 25000;
    public $hra = 5000;
    public $da = 3000;

    public function calculateSalary()
    {
        $totalSalary = $this->basicSalary
                      + $this->hra
                      + $this->da;

        echo "Basic Salary: Rs. " . $this->basicSalary . "<br>";
        echo "HRA: Rs. " . $this->hra . "<br>";
        echo "DA: Rs. " . $this->da . "<br>";
        echo "Total Salary: Rs. " . $totalSalary;
    }
}

$employee = new Employee();

$employee->calculateSalary();

?>

Sample Output

Basic Salary: Rs. 25000
HRA: Rs. 5000
DA: Rs. 3000
Total Salary: Rs. 33000

Explanation

  • The class contains three salary properties.
  • calculateSalary() adds all three values.
  • $this accesses the properties of the current employee object.
  • The calculated total is stored in $totalSalary.

Concepts Covered

  • Multiple properties
  • Mathematical calculations inside methods
  • Accessing properties with $this

Question 5: Create a Method That Returns a Value

Problem Statement

Create a Rectangle class with length and width properties. Create an area() method that returns the area of the rectangle.

PHP Solution

<?php

class Rectangle
{
    public $length = 10;
    public $width = 5;

    public function area()
    {
        return $this->length * $this->width;
    }
}

$rectangle = new Rectangle();

$result = $rectangle->area();

echo "Area of Rectangle: " . $result;

?>

Sample Output

Area of Rectangle: 50

Explanation

  • The Rectangle class contains two properties.
  • The area() method multiplies length by width.
  • The return statement sends the calculated value back to the calling code.
  • The returned value is stored in $result.

Concepts Covered

  • Methods with return values
  • Returning calculated results
  • Using returned values outside a method

Question 6: Create a Method with Default Parameter Values

Problem Statement

Create a Mobile class with a brand property. Create a method called displayMobile() that accepts a model name with a default value of "Unknown Model".

PHP Solution

<?php

class Mobile
{
    public $brand = "Samsung";

    public function displayMobile($model = "Unknown Model")
    {
        echo "Brand: " . $this->brand . "<br>";
        echo "Model: " . $model . "<br><br>";
    }
}

$mobile = new Mobile();

$mobile->displayMobile("Galaxy S25");

$mobile->displayMobile();

?>

Sample Output

Brand: Samsung
Model: Galaxy S25

Brand: Samsung
Model: Unknown Model

Explanation

  • displayMobile() accepts one parameter called $model.
  • Its default value is "Unknown Model".
  • When a model name is provided, PHP uses that value.
  • When no argument is provided, PHP uses the default value.

Concepts Covered

  • Method parameters
  • Default parameter values
  • Calling the same method with different arguments

Question 7: Create a Method to Apply a Discount

Problem Statement

Create a Product class with productName and price properties. Create an applyDiscount() method that accepts a discount percentage and returns the final price.

PHP Solution

<?php

class Product
{
    public $productName = "Laptop";
    public $price = 50000;

    public function applyDiscount($discount)
    {
        $discountAmount = ($this->price * $discount) / 100;

        return $this->price - $discountAmount;
    }
}

$product = new Product();

$finalPrice = $product->applyDiscount(10);

echo "Product: " . $product->productName . "<br>";
echo "Original Price: Rs. " . $product->price . "<br>";
echo "Final Price: Rs. " . $finalPrice;

?>

Sample Output

Product: Laptop
Original Price: Rs. 50000
Final Price: Rs. 45000

Explanation

  • The product price is Rs. 50,000.
  • The method receives a discount of 10%.
  • The discount amount is calculated as Rs. 5,000.
  • The method returns Rs. 45,000 as the final price.

Concepts Covered

  • Methods with parameters
  • Percentage calculations
  • Returning calculated values

Question 8: Create a Method to Update Bank Account Balance

Problem Statement

Create a BankAccount class with accountHolder and balance properties. Create a deposit() method to add money to the account balance.

PHP Solution

<?php

class BankAccount
{
    public $accountHolder = "Rahul";
    public $balance = 10000;

    public function deposit($amount)
    {
        $this->balance = $this->balance + $amount;

        echo "Deposited Amount: Rs. " . $amount . "<br>";
        echo "Updated Balance: Rs. " . $this->balance;
    }
}

$account = new BankAccount();

$account->deposit(5000);

?>

Sample Output

Deposited Amount: Rs. 5000
Updated Balance: Rs. 15000

Explanation

  • The initial balance is Rs. 10,000.
  • The deposit() method receives Rs. 5,000.
  • The amount is added to the existing balance.
  • The updated balance becomes Rs. 15,000.

Concepts Covered

  • Modifying object properties
  • Methods that change object state
  • Working with numeric values

Question 9: Create a Method to Display Student Marks and Percentage

Problem Statement

Create a Student class with name, english, maths and science properties. Create methods to calculate total marks, percentage and display student details.

Assume each subject carries 100 marks.

PHP Solution

<?php

class Student
{
    public $name = "Priya";
    public $english = 85;
    public $maths = 90;
    public $science = 80;

    public function calculateTotal()
    {
        return $this->english
             + $this->maths
             + $this->science;
    }

    public function calculatePercentage()
    {
        return ($this->calculateTotal() / 300) * 100;
    }

    public function displayDetails()
    {
        echo "Student Name: " . $this->name . "<br>";
        echo "Total Marks: " . $this->calculateTotal() . "<br>";
        echo "Percentage: " . $this->calculatePercentage() . "%";
    }
}

$student = new Student();

$student->displayDetails();

?>

Sample Output

Student Name: Priya
Total Marks: 255
Percentage: 85%

Explanation

  • calculateTotal() adds the marks of all three subjects.
  • calculatePercentage() uses the returned total to calculate the percentage.
  • displayDetails() calls both methods to display the student’s result.
  • One method can call another method using $this.

Concepts Covered

  • Multiple methods in one class
  • Calling methods from another method
  • Returning and reusing calculated values

Question 10: Create an Employee Salary Management Class

Problem Statement

Create an Employee class with name, basicSalary, hra, da and pf properties.

Create separate methods to calculate gross salary, net salary and display the employee’s salary details.

Formula:

  • Gross Salary = Basic Salary + HRA + DA
  • Net Salary = Gross Salary – PF

PHP Solution

<?php

class Employee
{
    public $name;
    public $basicSalary;
    public $hra;
    public $da;
    public $pf;

    public function setDetails($name, $basicSalary, $hra, $da, $pf)
    {
        $this->name = $name;
        $this->basicSalary = $basicSalary;
        $this->hra = $hra;
        $this->da = $da;
        $this->pf = $pf;
    }

    public function calculateGrossSalary()
    {
        return $this->basicSalary + $this->hra + $this->da;
    }

    public function calculateNetSalary()
    {
        return $this->calculateGrossSalary() - $this->pf;
    }

    public function displaySalary()
    {
        echo "Employee Name: " . $this->name . "<br>";
        echo "Basic Salary: Rs. " . $this->basicSalary . "<br>";
        echo "HRA: Rs. " . $this->hra . "<br>";
        echo "DA: Rs. " . $this->da . "<br>";
        echo "PF: Rs. " . $this->pf . "<br>";
        echo "Gross Salary: Rs. " . $this->calculateGrossSalary() . "<br>";
        echo "Net Salary: Rs. " . $this->calculateNetSalary();
    }
}

$employee = new Employee();

$employee->setDetails("Amit", 30000, 6000, 4000, 3000);

$employee->displaySalary();

?>

Sample Output

Employee Name: Amit
Basic Salary: Rs. 30000
HRA: Rs. 6000
DA: Rs. 4000
PF: Rs. 3000
Gross Salary: Rs. 40000
Net Salary: Rs. 37000

Explanation

  • setDetails() assigns the employee’s information to the object properties.
  • calculateGrossSalary() adds basic salary, HRA and DA.
  • calculateNetSalary() subtracts PF from the gross salary.
  • displaySalary() displays all salary details.
  • The class uses separate methods for separate responsibilities, making the code easier to understand and maintain.

Concepts Covered

  • Multiple properties and methods
  • Reusable methods
  • Method-to-method communication
  • Salary calculations
  • Managing object data

Key Takeaways

After completing this chapter, you should understand:

  • Properties store information inside a class.
  • Methods define actions that an object can perform.
  • $this is used to access the current object’s properties and methods.
  • Methods can accept parameters.
  • Methods in PHP can have default parameter values.
  • The return statement sends a result back from a method.
  • One method can call another method.
  • Different objects can store different property values.
  • Methods in PHP can update an object’s properties.

Frequently Asked Questions (FAQs)

1. What are properties in PHP?

Properties are variables declared inside a class. They store information about an object, such as employee name, salary, product price or student marks.

2. What are methods in PHP?

Methods are functions declared inside a class. They define the actions that an object can perform, such as displaying details or calculating salary.

3. What is the difference between properties and methods?

Properties store data, whereas methods perform operations. For example, an employee’s name is a property, while calculating the employee’s salary is a method.

4. What is the $this keyword in PHP?

The $this keyword refers to the current object. It allows a method to access the properties and other methods belonging to that object.

5. Can a PHP class have multiple properties and methods?

Yes. A PHP class can contain multiple properties and methods depending on the requirements of the program.

6. Can a method accept parameters in PHP?

Yes. A method can accept one or more parameters to receive information when it is called.

7. Can a PHP method return a value?

Yes. A method can use the return statement to send a calculated or stored value back to the code that called it.

8. Can one method call another method inside the same class?

Yes. You can call another method using $this->methodName() from within the class.

9. Can two objects have different property values?

Yes. Each object maintains its own instance property values. Updating a property in one object does not automatically change that property in another object.

10. Can a method change the value of a property?

Yes. A method can update an object’s property using $this->propertyName.

Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.

Scroll to Top