6. Objects Flashcards
How do you call static methods on a class?
ClassName::methodName();
How do you make a true copy of an object? What do you need to consider about the clone copy depth?
$copy = clone $original;
Consider that clone will shallow copy all properties, after the shallow cloning is complete it will call the __clone() magic method, which you can then clone any properties that need it giving you a deep copy.
How do you call an overridden method on an objects parent class? (Assume we’re in the child class)
parent::method();
COMMON MISTAKE is to use the parents class name like… Creature::method() but this is wrong, use parent::method()
In an object that might be subclassed, how do you ensure the method you’re calling is the current class?
self::method();
How would you check if an object is a particular class, or implements a particular interface?
if ($object instanceof ClassOrInterface)
When dealing with objects, what’s the purpose of traits?
Provide a mechanism for reusing code outside of a class heirarchy. You can share functionality across classes that shouldn’t share a common ancestor.
How do you define and then use a trait on a class?
How do you define a trait that is composed of multiple other traits?
If using multiple traits that define the same method, php compiler will throw a fatal error but you can REPLACE it, how do you do this?
telling the compile which specific method you’d like to use.
When using multiple traits with the same method names, instead of replacing we can also ALIAS the trait methods how do we do this?
How do you define an objects constructor?
function __construct($params) {}
Does PHP automatically call the parent class constructor? How can you explicitly call it?
No it doesn’t, we need to call parent::__construct($params);
How do you create anonymous class? Why is this useful?
Useful for testing.