Recall & Review
beginner
What does the
new keyword do in PHP?The <code>new</code> keyword creates a new instance (object) of a class. It tells PHP to build a fresh object based on the class blueprint.Click to reveal answer
beginner
How do you create an object from a class named <code>Car</code>?You write
$myCar = new Car();. This makes a new Car object and stores it in the variable $myCar.Click to reveal answer
beginner
What is the difference between a class and an object?A class is like a blueprint or recipe. An object is the actual thing made from that blueprint. Using <code>new</code> turns the blueprint into a real object you can use.Click to reveal answer
beginner
Can you instantiate a class without using <code>new</code> in PHP?No, in PHP you must use <code>new</code> to create an object from a class. Without <code>new</code>, you only have the class definition, not an object.Click to reveal answer
intermediate
What happens if you call <code>new</code> on a class with a constructor that requires parameters?You must provide the required parameters inside the parentheses. For example,
new Car('red', 2020);. Otherwise, PHP will give an error.Click to reveal answer
What does
$obj = new MyClass(); do in PHP?✗ Incorrect
Using
new creates a new object from the class named MyClass.Which keyword is used to create an object from a class in PHP?
✗ Incorrect
The
new keyword is used to instantiate (create) objects from classes.What will happen if you forget parentheses when instantiating a class like
new Car;?✗ Incorrect
In PHP, parentheses are optional if the constructor has no parameters, so
new Car; works.If a class constructor requires parameters, what must you do when using
new?✗ Incorrect
You must provide the required parameters inside the parentheses when calling
new.What is stored in the variable after
$obj = new ClassName();?✗ Incorrect
The variable holds the new object created from the class.
Explain in your own words what happens when you use the
new keyword in PHP.Think about turning a recipe into a cake.
You got /4 concepts.
Describe how you would create an object from a class that has a constructor with parameters.
Remember how you give ingredients when baking.
You got /4 concepts.