Classes and Objects Flashcards

1
Q

What is a valid class name?

A

A valid class name starts with a letter or underscore, followed by any number of letters, numbers, or underscores.

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

What can a class contain?

A

Its own constants, variables, and functions (called “methods”).

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

What is the pseudo-variable $this?

A

It’s a reference to the calling object (usually the object to which the method belongs, but possibly another object, if the method is called statically from the context of a secondary object).

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

When will the new keyword not create an instance of a class?

A

When the object has a constructor defined that throws an exception on error.

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

When a class is in a namespace, how can a new instance of that class be created?

A

Using its fully qualified name.

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

In the class context, it is possible to create a new object by…

A

new self and new parent

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

When assigning an already created instance of a class to a new variable, the new variable will access…

A

…the same instance as the object that was assigned.

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

What is the extends keyword?

A

A class can inherit the methods and properties of another class by using the keyword extends in the class declaration. It is not possible to extend multiple classes; a class can only inherit from one base class.

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

How can you override inherited methods and properties/

A

Redeclare them with the same name defined in the parent class. However, if the parent class has defined a method as final, that method may not be overridden. It is possible to access the overridden methods or static properties by referencing them with parent::.

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

When overriding methods, the parameter signature should…

A

…remain the same or PHP will generate an E_STRICT level error. This does not apply to the constructor, which allows overriding with different parameters.

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

You can get a string containing the fully qualified name of the ClassName class by using…

A

…ClassName::class

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

What are properties?

A

Class member variables. They are also referred to as “attributes” and “fields”.

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

How are properties defined?

A

They are defined by using one of the keywords public, protected, or private, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value–that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

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

What happens if you declare a property using var instead of one of public/private/protected?

A

PHP 5 will treat the property as if it had been declared as public.

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

Can heredocs be used in a static data context, including property declarations?

A

No, but nowdocs can.

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

What are the limits on defining constants on a per-class basis?

A

The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.

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

Can you reference a class using a variable?

A

Yes, but the variable’s value can not be a keyword (e.g. self, parent and static).

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

What is an __autoload() function?

A

A function which is automatically called in case you are trying to use a class/interface which hasn’t been defined yet. By calling this function the scripting engine is given a last chance to load the class before PHP fails with an error.

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

Why is using __autoload() discouraged?

A

spl_autoload_register() provides a more flexible alternative for autoloading classes. For this reason, using __autoload() is discouraged and may be deprecated or removed in the future.

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

Can exceptions thrown in the __autoload() function be caught in the catch block?

A

Yes, with one provision: if throwing a custom exception, then the custom exception class must be available. The __autoload function may be used recursively to autoload the custom exception class.

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

Is autoloading available if using PHP in CLI interactive mode?

A

No.

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

What is a constructor method?

A

A method which is called on each newly-created object.

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

How can you run a parent constructor?

A

Call parent::__construct() within the child constructor. If the child does not define a constructor, it may be inherited from the parent class just like a normal class method (if it was not declared private).

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

Are methods with the same name as the last element of a namespaced class name treated as constructor?

A

No, not any longer.

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

What is a destructor method?

A

A method called as soon as there are no other references to a particular object.

void __destruct(void)

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

Are parent destructors called implicitly in a child class?

A

No, one would have to explicitly call parent::__destruct() in the destructor body. Also like constructors, a child class may inherit the parent’s destructor if it does not implement one itself.

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

Is a destructor still called if script execution is halted with a call to exit()?

A

Yes.

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

What are the differences between the public, private, and protected keywords?

A
Public: can be accessed anywhere.
Private: may only be accessed by the class that defines the member.
Protected: can be accessed only within the class itself, and by inherited and parent classes.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
29
Q

If a class property is declared using “var”, is it defined as public, protected, or private?

A

Public.

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

What is the visibility of methods declared without any explicit visibility keyword?

A

Public.

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

Can public and protected methods be redeclared?

A

Yes, but not private methods.

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

What is the scope resolution operator?

A

A double colon token that allows access to static, constant, and overridden properties or methods of a class.

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

Can you reference a class using a variable?

A

Yes, as long as the variable value is not a keyword (e.g. self, parent, and static).

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

How can you call a parent function from within a class?

A

parent::myFunc()

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

What does declaring class properties or methods as static do?

A

It makes them accessible without needing an instantiation of the class.

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

If you declare a property as static, can you access it with an instantiated class object?

A

No, you can’t. However, a static method can access it.

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

Is the pseudo-variable $this available inside a method declared as static?

A

No.

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

Can you access static properties through an object using the arrow operator: -> ?

A

No.

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

What happens if you call non-static methods statically?

A

An E_STRICT level warning is generated.

40
Q

Can you use expressions to initialize static properties?

A

No, only a literal or a constant. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

41
Q

What are abstract classes and methods?

A

Classes defined as abstract may not be substantiated. Any class that contains at least one abstract method must also be abstract.

Methods defined as abstract simply define the method’s signature - they cannot define the implementation.

42
Q

If inheriting from an abstract class, what must occur?

A

All methods marked abstract in the parent’s class declaration must be defined by the child. Additionally, these methods must be defined with the same (or a less restricted) visibility.

Also, the signatures of the methods must match (type hints and number of required arguments).

43
Q

What are object interfaces?

A

They allow you to create code which specifies which methods a class must implement, without having to define how those methods are handled. They are defined using the ‘interface’ keyword.

44
Q

All methods defined in an interface must be…

A

…public.

45
Q

How do you implement an interface?

A

With the ‘implements’ keyword. All methods in the interface must be implemented in the class, with the exact same method signatures; failure to do so will result in a fatal error. Classes may implement more than one interface if desired, by separating each interface with a comma.

46
Q

Can interfaces be extended like classes?

A

Yes, with the ‘extends’ operator.

47
Q

Can interfaces have constants?

A

Yes. They work exactly like class constants, except they cannot be overridden by a class/interface that inherits them.

48
Q

What are traits?

A

Traits are a mechanism of code reuse that allow a developer to reuse sets of methods freely in several independent classes living in different class hierarchies.

Traits allow the application of class members without requiring inheritance.

49
Q

Can a trait be instantiated on its own?

A

No.

50
Q

Is an inherited member from a base class overridden by a member inserted by a trait?

A

Yes. The precedence order is that members from the current class override trait methods, which in turn override inherited methods.

51
Q

How can multiple traits be inserted into a class?

A

List them in the use statement, separated by commas.

52
Q

What happens if two traits insert a method with the same name?

A

A fatal error is produced, if the conflict is not explicitly resolved.

53
Q

How can you resolve naming conflicts between traits used in the same class?

A

By using the insteadof operator to choose exactly one of the conflicting methods.

54
Q

What is the ‘as’ operator?

A

It allows the inclusion of one of the conflicting trait methods under another name. It can also adjust the visibility of a method in an exhibiting class.

55
Q

Can traits make use of other traits?

A

Yes.

56
Q

What is an exhibiting class?

A

One that makes use of a trait.

57
Q

Can traits define abstract methods?

A

Yes, and they impose the standard requirements upon the exhibiting classes.

58
Q

Can traits define static members and methods?

A

Yes.

59
Q

Can traits define properties?

A

Yes. However, an exhibiting class cannot define a property with the same name, otherwise an error is issued. It is an E_STRICT if the class definition is compatible (same visibility and initial value) or fatal error otherwise.

60
Q

What is ‘overloading’ in PHP?

A

It provides means to dynamically “create” properties and methods. These dynamic entities are processed via magic methods one can establish in a class for various action types.

61
Q

When are the overloading methods invoked?

A

When interacting with properties or methods that have not been declared or are not visible in the current scope.

62
Q

What is the visibility for overloading methods?

A

All overloading methods must be defined as public.

63
Q

Can the arguments to overloading magic methods be passed by reference?

A

No.

64
Q

What happens if one of the magic overloading methods is declared static?

A

A warning is issued.

65
Q

Property overloading only works in ______ context.

A

Object.

66
Q

What are the property overloading functions?

A

__get(), __set(), __isset(), __unset()

67
Q

What are the method overloading functions?

A

__call(), __callStatic()

68
Q

When is __call() triggered?

A

When invoking inaccessible methods in an object context.

69
Q

When is __callStatic() triggered?

A

When invoking inaccessible methods in a static context.

70
Q

What are 4 ways to iterate through properties in a class, for example, with a foreach statement?

A
  1. Foreach $class as $key => $value will iterate over all public properties as varname => value.
  2. Add an iterate method inside the class. Same as above, but will also iterate over all private and protected properties.
  3. Implement the Iterator interface, which allows the object to dictate how it will be iterated and what values will be available on each iteration.
  4. Implement the IteratorAggregate interface, which only requires implementation of a single method.
71
Q

What are the PHP magic methods?

A
\_\_construct()
\_\_destruct()
\_\_call()
\_\_callStatic()
\_\_get()
\_\_set()
\_\_isset()
\_\_unset()
\_\_sleep()
\_\_wakeup()
\_\_toString()
\_\_invoke()
\_\_set_state()
\_\_clone()
\_\_debugInfo()

You cannot have functions with these names in any of your classes unless you want the magic functionality associated with them.

PHP reserves all function names starting with __ as magical. It is recommended that you do not use function names with __ in PHP unless you want some documented magic functionality.

72
Q

Can __sleep() return names of private properties in parent classes?

A

No. Doing this will result in an E_NOTICE level error.

73
Q

How does serialize() work with __sleep()?

A

serialize() checks if your class has a function with the magic name __sleep(). If so, that function is executed prior to any serialization. It can clean up the object and is supposed to return an array with the names of all variables of that object that should be serialized. If the method doesn’t return anything then NULL is serialized and E_NOTICE is issued.

74
Q

What is the intended use of __sleep()?

A

The intended use of __sleep() is to commit pending data or perform similar cleanup tasks. Also, the function is useful if you have very large objects which do not need to be saved completely.

75
Q

How does unserialize() work with __wakeup()?

A

unserialize() checks for the presence of a function with the magic name __wakeup(). If present, this function can reconstruct any resources that the object may have.

76
Q

What is the intended use of __wakeup()?

A

To reestablish any database connections that may have been lost during serialization and perform other reinitialization tasks.

77
Q

What does the __toString() magic method do?

A

The __toString() method allows a class to decide how it will react when it is treated like a string. For example, what echo $obj; will print. This method must return a string, as otherwise a fatal E_RECOVERABLE_ERROR level error is emitted.

78
Q

Can you throw an exception from within a __toString() method?

A

No. Doing so will result in a fatal error.

79
Q

What does __invoke() do?

A

The __invoke() method is called when a script tries to call an object as a function.

80
Q

What does __set_state() do?

A

This static method is called for classes exported by var_export() since PHP 5.1.0.

The only parameter of this method is an array containing exported properties in the form array(‘property’ => value, …).

81
Q

What does __debugInfo() do?

A

This method is called by var_dump() when dumping an object to get the properties that should be shown. If the method isn’t defined on an object, then all public, protected and private properties will be shown.

82
Q

What is the final keyword used for?

A

It prevents child classes from overriding a method by prefixing the definition with “final”. If the class itself is being defined as final, then it cannot be extended. Doing so would result in a fatal error.

83
Q

Can properties be declared final?

A

No, only classes and methods.

84
Q

How can you copy an object?

A

With the “clone” keyword. When an object is cloned, PHP 5 will perform a shallow copy of all of the object’s properties. Any properties that are references to other variables, will remain references.

85
Q

What is the __clone() method?

A

It’s called when the clone keyword is used, after the cloning is complete. It can’t be called directly.

86
Q

When are two object instances equal per the comparison operator ( == )? What about identical?

A

They are equal when they have the same attributes and values, and are instances of the same class.

They are identical only when they refer to the same instance of the same class.

87
Q

With type hinting, what can you force parameters to be?

A

Objects, interfaces, arrays, or callable.

88
Q

If a class or an interface is specified as a type hint, are its children or implementations allowed too?

A

Yes.

89
Q

Failing to satisfy the type hint results in a…

A

…catchable fatal error.

90
Q

If you make a method in a parent class, and call it in a child class, what does the “self” keyword reference?

A

The parent.

91
Q

What are late static bindings?

A

They are used to reference the called class in a context of static inheritance. With the “static” keyword, you can reference the class that was initially called at runtime.

92
Q

By default, are objects passed by value or reference?

A

Reference.

93
Q

Does an object variable contain an object itself as a value?

A

No, it contains an object identifier which allows object accessors to find the actual object.

94
Q

When an object is sent by argument, returned or assigned to another variable, are the different variables aliases?

A

No. They hold a copy of the identifier, which points to the same object.

95
Q

What does using serialize() to save an object do?

A

It saves all variables in an object, as well as the name of the class, but not all methods.

96
Q

In order to be able to unserialize() an object…

A

…the class of that object needs to be defined. This can be done for example by storing the class definition of class A in an include file and including this file or making use of the spl_autoload_register() function.