6. Objects Flashcards

1
Q

How do you call static methods on a class?

A

ClassName::methodName();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How do you make a true copy of an object? What do you need to consider about the clone copy depth?

A

$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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

How do you call an overridden method on an objects parent class? (Assume we’re in the child class)

A

parent::method();

COMMON MISTAKE is to use the parents class name like… Creature::method() but this is wrong, use parent::method()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In an object that might be subclassed, how do you ensure the method you’re calling is the current class?

A

self::method();

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How would you check if an object is a particular class, or implements a particular interface?

A

if ($object instanceof ClassOrInterface)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

When dealing with objects, what’s the purpose of traits?

A

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 well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do you define and then use a trait on a class?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do you define a trait that is composed of multiple other traits?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

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?

A

telling the compile which specific method you’d like to use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

When using multiple traits with the same method names, instead of replacing we can also ALIAS the trait methods how do we do this?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do you define an objects constructor?

A

function __construct($params) {}

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Does PHP automatically call the parent class constructor? How can you explicitly call it?

A

No it doesn’t, we need to call parent::__construct($params);

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

How do you create anonymous class? Why is this useful?

A

Useful for testing.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly