Practice 50 PHP Classes Programming Questions, TechnoVlogs

Practice 50 PHP Classes Programming Questions


Q1. Write a PHP program to define a simple class and create an object of that class.  
Input: Class name = Person, Object name = $person  
Expected Output: "Object of class Person created successfully"

Q2. Write a PHP program to create a class with a property and set its value.  
Input: Class = Car, Property = color, Value = Red  
Expected Output: "The car's color is Red"

Q3. Write a PHP program to create a class with a method that prints a message.  
Input: Class = Greeting, Method = sayHello(), Message = Hello, World!  
Expected Output: "Hello, World!"

Q4. Write a PHP program to define a class with a constructor that initializes properties.  
Input: Class = Book, Properties = title, author, Values = 1984, George Orwell  
Expected Output: "Book title: 1984, Author: George Orwell"

Q5. Write a PHP program to create a class with a private property and a public method to access it.  
Input: Class = Employee, Property = salary, Value = 5000  
Expected Output: "The salary is 5000"

Q6. Write a PHP program to define a class with a static property and access it without creating an object.  
Input: Class = Counter, Static Property = count, Value = 10  
Expected Output: "Static count value is 10"

Q7. Write a PHP program to create a class with a method that takes arguments and returns the result.  
Input: Class = Calculator, Method = add($a, $b), Values = 10, 20  
Expected Output: "The sum is 30"

Q8. Write a PHP program to create a class with a constant and access it from the object.  
Input: Class = MathConstants, Constant = PI, Value = 3.14159  
Expected Output: "The value of PI is 3.14159"

Q9. Write a PHP program to create a class with a method that multiplies two numbers.  
Input: Class = Multiplier, Method = multiply($a, $b), Values = 4, 5  
Expected Output: "The result is 20"

Q10. Write a PHP program to create a class with a default constructor and call it.  
Input: Class = Welcome  
Expected Output: "Welcome to the PHP class"

Q11. Write a PHP program to create a class and override its method in a child class.  
Input: Parent Class = Shape, Method = area(), Child Class = Rectangle, Override Method to return width  height  
Expected Output: "Area of the rectangle is calculated"

Q12. Write a PHP program to define a class with multiple properties and display their values.  
Input: Class = Product, Properties = name, price, Values = Laptop, 50000  
Expected Output: "Product name: Laptop, Price: 50000"

Q13. Write a PHP program to use a destructor in a class to print a message when the object is destroyed.  
Input: Class = Demo  
Expected Output: "Object of class Demo is destroyed"

Q14. Write a PHP program to create a class with a static method and call it without creating an object.  
Input: Class = Utility, Static Method = getCurrentTime()  
Expected Output: "Current time: 12:00 PM"

Q15. Write a PHP program to create a class with a protected property and access it in a child class.  
Input: Parent Class = Animal, Property = type, Value = Mammal, Child Class = Dog  
Expected Output: "This animal is a Mammal"

Q16. Write a PHP program to use the self keyword to access a static property within a class.  
Input: Class = Counter, Static Property = count  
Expected Output: "Count is 0"

Q17. Write a PHP program to use the parent keyword to call a method from the parent class.  
Input: Parent Class = Vehicle, Method = start(), Child Class = Car  
Expected Output: "Vehicle started"

Q18. Write a PHP program to define a class and use getters and setters for its properties.  
Input: Class = User, Property = name, Value = John  
Expected Output: "User name is John"

Q19. Write a PHP program to implement a class that calculates the area of a circle.  
Input: Class = Circle, Property = radius, Value = 7  
Expected Output: "Area of the circle is 153.93804"

Q20. Write a PHP program to create a class that contains another class object as a property.  
Input: Outer Class = Company, Inner Class = Employee, Employee Name = Alice  
Expected Output: "Company has an employee named Alice"

Q21. Write a PHP program to create a class and use its object in a function as an argument.  
Input: Class = Person, Property = age, Value = 25  
Expected Output: "Person's age is 25"

Q22. Write a PHP program to create a class with an abstract method and implement it in a derived class.  
Input: Abstract Class = Shape, Method = area(), Child Class = Square  
Expected Output: "Area of the square calculated"

Q23. Write a PHP program to implement a class with a trait.  
Input: Trait = Logger, Class = Application, Method = logMessage()  
Expected Output: "Message logged"

Q24. Write a PHP program to create a class that uses interfaces to define methods.  
Input: Interface = Movable, Class = Car, Method = move()  
Expected Output: "Car is moving"

Q25. Write a PHP program to implement method chaining in a class.  
Input: Class = Calculator, Methods = add(), subtract()  
Expected Output: "Result after method chaining is 20"

Q26. Write a PHP program to create a class and call its method multiple times in a loop.  
Input: Class = Counter, Method = increment()  
Expected Output: "Counter value after 3 increments is 3"

Q27. Write a PHP program to create a class with a final method.  
Input: Class = BaseClass, Method = show()  
Expected Output: "Final method cannot be overridden"

Q28. Write a PHP program to create a singleton class.  
Input: Class = Singleton, Method = getInstance()  
Expected Output: "Single instance of the class created"

Q29. Write a PHP program to use the __toString method in a class.  
Input: Class = Product, Properties = name, price, Values = Phone, 20000  
Expected Output: "Product: Phone, Price: 20000"

Q30. Write a PHP program to create a class with dynamic property addition.  
Input: Class = Dynamic, Property = newProp, Value = Hello  
Expected Output: “New property added with value Hello”

Q31. Write a PHP program to create a class with a constructor that accepts multiple arguments.  
Input: Class = Person, Arguments = name, age, Values = John, 30  
Expected Output: "Name: John, Age: 30"

Q32. Write a PHP program to implement property overloading using the __get and __set magic methods.  
Input: Class = PropertyOverload, Property = data, Value = 100  
Expected Output: "Value of data is 100"

Q33. Write a PHP program to implement a class that inherits from a parent class and adds new properties.  
Input: Parent Class = Vehicle, Child Class = Bike, Property = type, Value = Motorbike  
Expected Output: "This vehicle is a Motorbike"

Q34. Write a PHP program to create a class and clone its object using the __clone method.  
Input: Class = Car, Property = brand, Value = Toyota  
Expected Output: "Original object and cloned object have brand: Toyota"

Q35. Write a PHP program to create a class that handles invalid method calls using the __call magic method.  
Input: Class = DynamicMethod, Method = nonExistentMethod()  
Expected Output: "Method nonExistentMethod does not exist"

Q36. Write a PHP program to create a class that uses the __isset and __unset magic methods for properties.  
Input: Class = MyClass, Property = name, Value = PHP  
Expected Output: "Property name exists and is unset"

Q37. Write a PHP program to implement multiple interfaces in a class.  
Input: Interfaces = Readable, Writable, Class = File, Methods = read(), write()  
Expected Output: "File is readable and writable"

Q38. Write a PHP program to use the final keyword to prevent a class from being extended.  
Input: Final Class = Immutable  
Expected Output: "Class Immutable cannot be extended"

Q39. Write a PHP program to create a class and define an array property that can be iterated.  
Input: Class = Team, Array Property = players, Values = Alice, Bob, Charlie  
Expected Output: "Player: Alice, Player: Bob, Player: Charlie"

Q40. Write a PHP program to create a parent class and a child class that calls the parent's constructor using the parent::__construct() method.  
Input: Parent Class = Person, Properties = name, age, Child Class = Student, Additional Property = grade  
Expected Output: "Name: John, Age: 20, Grade: A"

Q41. Write a PHP program to define a class with a method that calculates the factorial of a number.  
Input: Class = MathOperations, Method = factorial($n), Value = 5  
Expected Output: "Factorial of 5 is 120"

Q42. Write a PHP program to create a class that counts how many objects are created using a static property.  
Input: Class = ObjectCounter  
Expected Output: "Total objects created: 3"

Q43. Write a PHP program to create a class that validates email addresses using a method.  
Input: Class = EmailValidator, Method = isValid($email), Value = example@test.com  
Expected Output: "example@test.com is a valid email address"

Q44. Write a PHP program to define a class that implements a simple calculator with addition and subtraction methods.  
Input: Class = SimpleCalculator, Methods = add($a, $b), subtract($a, $b), Values = 10, 5  
Expected Output: "Addition result: 15, Subtraction result: 5"

Q45. Write a PHP program to implement a class with a method that reverses a string.  
Input: Class = StringUtility, Method = reverse($str), Value = Hello  
Expected Output: "Reversed string: olleH"

Q46. Write a PHP program to create a class with a method that checks if a number is even or odd.  
Input: Class = NumberCheck, Method = isEven($num), Value = 8  
Expected Output: "8 is even"

Q47. Write a PHP program to define a class that implements a simple login system with a username and password.  
Input: Class = LoginSystem, Method = authenticate($username, $password), Values = admin, 1234  
Expected Output: "Login successful"

Q48. Write a PHP program to define a class with an array property and a method to add elements to the array.  
Input: Class = Cart, Array Property = items, Method = addItem($item), Value = Laptop  
Expected Output: "Item Laptop added to cart"

Q49. Write a PHP program to implement a class that generates random numbers using a method.  
Input: Class = RandomGenerator, Method = generate($min, $max), Values = 1, 10  
Expected Output: "Random number: 7"

Q50. Write a PHP program to create a class with a method that converts Celsius to Fahrenheit.  
Input: Class = TemperatureConverter, Method = toFahrenheit($celsius), Value = 25  
Expected Output: "25°C is equal to 77°F"

Social Share

Bikki Singh Instructor TechnoVlogs

Bikki Singh

Hi, I am the instructor of TechnoVlogs. I have a strong love for programming and enjoy teaching through practical examples. I made this site to help people improve their coding skills by solving real-world problems. With years of experience, my goal is to make learning programming easy and fun for everyone. Let's learn and grow together!