0
0
PHPprogramming~5 mins

Object instantiation with new in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ACreates a new object of MyClass
BDeletes the MyClass definition
CCalls a function named MyClass
DAssigns a string 'MyClass' to $obj
Which keyword is used to create an object from a class in PHP?
Acreate
Bobject
Cnew
Dmake
What will happen if you forget parentheses when instantiating a class like new Car;?
AIt will call the class as a function
BIt will cause a syntax error
CIt will create a string instead
DIt will create the object anyway
If a class constructor requires parameters, what must you do when using new?
ACall the constructor separately after new
BPass the required parameters inside parentheses
CNo parameters are needed
DUse a different keyword than new
What is stored in the variable after $obj = new ClassName();?
AAn object instance of ClassName
BThe class code
CA string 'ClassName'
DNothing, it's empty
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.