📖 Scenario: You are creating a simple program to manage a library's book information. Each book has a title and an author.
🎯 Goal: Build a PHP program that defines a Book class, creates an object of this class using new, and displays the book's details.
📋 What You'll Learn
Define a class named Book with two public properties: title and author.
Create an object of the Book class using the new keyword.
Assign values to the title and author properties of the object.
Print the book's title and author in the format: Title: [title], Author: [author].
💡 Why This Matters
🌍 Real World
Creating and managing objects is a basic skill in PHP used in web applications to represent real-world things like users, products, or books.
💼 Career
Understanding object instantiation is essential for PHP developers working on object-oriented projects, frameworks, and CMS platforms.
Progress0 / 4 steps
1
Define the Book class
Define a class called Book with two public properties: title and author.
PHP
Hint
Use class Book { public string $title; public string $author; } to define the class.
2
Create a Book object
Create an object called myBook of the Book class using the new keyword.
PHP
Hint
Use $myBook = new Book(); to create the object.
3
Assign values to properties
Assign the string "1984" to myBook->title and "George Orwell" to myBook->author.
PHP
Hint
Use $myBook->title = "1984"; and $myBook->author = "George Orwell";.
4
Print book details
Print the book's title and author using print in the format: Title: 1984, Author: George Orwell.
PHP
Hint
Use print("Title: {$myBook->title}, Author: {$myBook->author}"); to display the details.
Practice
(1/5)
1.
What does the new keyword do in PHP?
easy
A. It deletes an existing object.
B. It defines a new class.
C. It calls a function inside a class.
D. It creates a new instance of a class (an object).
Solution
Step 1: Understand the role of new
The new keyword is used to create an object from a class blueprint.
Step 2: Differentiate from other keywords
It does not delete objects, call functions, or define classes; it only creates objects.
Final Answer:
It creates a new instance of a class (an object). -> Option D
Quick Check:
new creates objects [OK]
Hint: Remember: new = create object from class [OK]
Common Mistakes:
Thinking new deletes objects
Confusing new with function calls
Believing new defines classes
2.
Which of the following is the correct syntax to create a new object of class Car?
class Car {
// class body
}
// Create object here
easy
A. $car = new Car();
B. $car = Car();
C. new $car = Car();
D. $car = Car.new();
Solution
Step 1: Recall PHP object creation syntax
In PHP, to create an object, use $variable = new ClassName();.
Step 2: Check each option
$car = new Car(); matches the correct syntax. The other options use invalid syntax: new before variable assignment, omitting the new keyword, or using dot notation.
Final Answer:
$car = new Car(); -> Option A
Quick Check:
Use new ClassName() to create objects [OK]
Hint: Use $obj = new ClassName(); always [OK]
Common Mistakes:
Omitting the new keyword
Placing new after variable
Using dot notation like other languages
3.
What will be the output of the following PHP code?
class Dog {
public $name;
public function __construct($name) {
$this->name = $name;
}
public function bark() {
return "Woof! I am " . $this->name;
}
}
$dog = new Dog("Buddy");
echo $dog->bark();
medium
A. Woof! I am Buddy
B. Woof! I am $name
C. Error: Undefined property
D. Woof! I am
Solution
Step 1: Understand constructor usage
The constructor sets the name property to "Buddy" when the object is created.
Step 2: Analyze the bark method output
The bark() method returns "Woof! I am " concatenated with the name property, which is "Buddy".
Final Answer:
Woof! I am Buddy -> Option A
Quick Check:
Constructor sets name, bark returns it [OK]
Hint: Constructor sets data; methods use it [OK]
Common Mistakes:
Ignoring constructor parameters
Using variable name instead of property
Expecting syntax error due to missing parentheses
4.
Identify the error in this PHP code snippet:
class Person {
public $age;
public function __construct($age) {
$this->age = $age;
}
}
$person = Person(25);
echo $person->age;
medium
A. Property age should be private.
B. Constructor should not have parameters.
C. Missing new keyword when creating the object.
D. Cannot echo object property directly.
Solution
Step 1: Check object instantiation syntax
The object is created by calling Person(25) without new, which is invalid in PHP.
Step 2: Correct the instantiation
Use $person = new Person(25); to create the object properly.
Final Answer:
Missing new keyword when creating the object. -> Option C
Quick Check:
Always use new to instantiate objects [OK]
Hint: Always use new before class name to create object [OK]
Common Mistakes:
Forgetting new keyword
Thinking constructor can't have parameters
Confusing property visibility with errors
5.
Given this class:
class Book {
public $title;
public $author;
public function __construct($title, $author) {
$this->title = $title;
$this->author = $author;
}
}
// Create an array of books with titles and authors
$booksData = [
['1984', 'George Orwell'],
['Brave New World', 'Aldous Huxley'],
['Fahrenheit 451', 'Ray Bradbury']
];
// Fill this array with Book objects
$books = [];
foreach ($booksData as $data) {
// Fill here
}
Which line correctly creates and adds a new Book object to $books inside the loop?
hard
A. $books[] = new Book($data[1], $data[0]);
B. $books[] = new Book($data[0], $data[1]);
C. $books[] = new Book($data);
D. $books[] = Book($data[0], $data[1]);
Solution
Step 1: Understand constructor parameters
The constructor expects two parameters: title and author, in that order.
Step 2: Match data array to constructor
$data[0] is title, $data[1] is author, so new Book($data[0], $data[1]) is correct.
Step 3: Add object to array
Using $books[] = appends the new object to the array.
Final Answer:
$books[] = new Book($data[0], $data[1]); -> Option B
Quick Check:
Use new with correct params and append [OK]
Hint: Match constructor params order exactly when using new [OK]
Common Mistakes:
Omitting new keyword
Passing whole array instead of separate params
Swapping title and author order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.