Classes and Objects Flashcards

1
Q

What can a class name be?

A

Any valid label, provided it is not a PHP reserved word.

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

A valid class name starts with…

A

…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
3
Q

What can a class contain?

A

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

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

What is $this, and when is it available?

A

$this is available when a method is called from within an object context. 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
5
Q

When will an object not be created when using the ‘new’ keyword?

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
6
Q

Do classes need to be defined before instantiation?

A

It’s not a requirement, but they should be.

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

What happens when a string containing the name of a class is used with the ‘new’ keyword?

A

A new instance of that class will be created. If the class is in a namespace, its fully qualified name must be used when doing this.

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

How can you create a new object in the class context?

A

By using ‘new self’ and ‘new parent’.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
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 (like passing by reference). This behavior is the same when passing instances to a function. It can be negated by using the ampersand to assign by reference.

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

How can you make a copy of an already-created object?

A

You can clone it.

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

How can a class inherit the methods and properties of another class?

A

By using the ‘extends’ keyword in the class declaration.

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

How many levels of classes can you extend?

A

Just one; a class can only inherit from one base class.

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

When can a parent class’s method not be overridden?

A

When it was declared as ‘final’.

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

How can the overridden methods of a parent class be accessed?

A

By referencing them with parent::

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

What happens when you override a method, but use a different parameter signature?

A

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
16
Q

How can you get a string containing the fully qualified name of the ClassName class?

A

ClassName::class

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

What is the difference between $this and self?

A

Inside a class definition, $this refers to the current object, while self refers to the current class.

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

What is stdClass?

A

It’s the default PHP object. It has no properties, methods, or parent. It does not support magic methods, and implements no interface. When you cast a scalar or array as Object, you get an instance of stdClass.

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

What are the three different names for class member variables?

A

Properties, attributes, and fields.

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

Is the keyword ‘var’ required in property declarations?

A

No, it’s deprecated.

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

Can property values in a class include an initialization:

$a = 1;

A

Yes, but the initialization must be a constant value; 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
22
Q

If you declare a property using ‘var’, instead of public/protected/private, then PHP5 will treat the property as if it had been declared as…

A

…public.

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

Can heredocs be used in property declarations?

A

No, but nowdocs can.

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

Can you define constant values on a per-class basis?

A

Yes. 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
25
Q

Can a class be referenced using a variable?

A

Yes, but the variable’s value can not be a keyword.

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

What is the visibility of class constants?

A

They are always publicly visible; they can’t be made private or protected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
27
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 (PHP would otherwise fail with an error).

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

Why is using __autoload() discouraged?

A

It may be deprecated or removed in the future. spl_autoload_register() provides a more flexible alternative for autoloading classes.

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

When is Autoloading not available?

A

When using PHP in CLI interactive mode.

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

If a child class defines a constructor, is a parent class called implicitly?

A

No. It must be explicitly referenced with a call to parrent::__construct(), within the child constructor.

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

If a child class does not define a constructor, can it be inherited from the parent class

A

Yes, as long as it was not declared private.

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

What will PHP do if it cannot find a __construct function for a given class, and the class did not inherit a parent one?

A

It will search for the old-style constructor function, by the name of the class.

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

When happens __construct() is overridden with different parameters than the parent __construct() method has?

A

PHP will NOT generate an E_STRICT level error message.

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

As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name…

A

… will no longer be treated as constructor. This change doesn’t affect non-namespaced classes.

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

What is __destruct()?

A

It’s a method that’s called as soon as there are no other references to a particular object, or in any order during the shutdown sequence.

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

Are parent destructors implicitly called by the engine?

A

No; you have to explicitly call parent::__destruct() in the destructor body.

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

Can destructors be inherited from a parent class?

A

Yes, they can, just like constructors.

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

If script execution is stopped with exit(), are destructors still called.

A

Yes. However, calling exit() from within a destructor will prevent the remaining shutdown routines from executing.

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

Can you throw an exception from a destructor?

A

No; it causes a fatal error (when called in the time of script termination).

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

How can the visibility of a property or method be defined?

A

By prefixing the declaration with the keywords public, protected, or private.

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

What are the definitions of the visibility keywords?

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

Must a visibility keyword be used to define a class property?

A

Yes. If declared using ‘var’, the property will be defined as public.

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

Must a visibility keyword be used to define a class method?

A

No. Methods without any explicit visibility keyword are defined as public.

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

Do objects of the same type have access to each others’ private and protected members even though they are not the same instances?

A

Yes they do.

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

Can classes be used before they are defined?

A

Not unless autoloading is used.

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

If a class extends another, can the parent class be declared after the child class structure?

A

No. This rule applies to classes that inherit other classes and interfaces.

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

How can you force a class to be strictly an inheritable class?

A

Use the ‘abstract’ keyword. Then any attempt to instantiate the class directly will result in a fatal error.

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

What is the scope operator also called?

A

Paamayim Nekudotayim

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

Can a class be referenced using a variable?

A

Yes, but that variable cannot be a keyword (self, static, parent).

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

Can a class constant, static class property, and static class function all share the same name and be accessed with the scope operator?

A

Yes.

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

What are the three uses of the static keyword?

A
  1. Defining static methods and properties.
  2. Defining static variables.
  3. Late static bindings.
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
53
Q

What is the advantage of declaring class properties or methods as static?

A

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

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

Can a property declared as static be accessed with an instantiated class object?

A

No, but a static method can.

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

Can static properties be accessed using the arrow operator: -> ?

A

No.

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

What happens when you call non-static methods statically?

A

It generates an E_STRICT level warning.

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

Can static properties be initialized with an expression?

A

No. They may only be initialized using a literal or a constant. So, for example, you can initialize a static property to an integer or array, but not a variable, function return value, or an object.

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

Which classes MUST be abstract?

A

Any class that contains at least one abstract method.

60
Q

When inheriting from an abstract class, all methods marked abstract in the parent’s class definition must be…

A

…defined by the child. And they must be defined with the same (or a less restricted) visibility. And the signatures of the methods (type hints and number of required arguments) must match (this also applies to constructors). Optional arguments can differ.

61
Q

What’s the difference between an abstract class and an interface?

A

An interface is like a promise. When a class implements an interface, it promises to have the same public methods that any object with that interface has.

An abstract class is like a partially built class, or a document with blanks to fill in. When a class extends an abstract class, it uses some of the properties or methods already defined in the abstract class.

62
Q

Can you invoke a static method of an abstract class?

A

Yes.

63
Q

What are object interfaces?

A

They allow you to create code that specifies which methods a class must implement, without having to define how these methods are handled.

64
Q

How are interfaces defined?

A

With the ‘interface’ keyword, in the same way as a standard class, but without any of the methods having their contents defined.

65
Q

Describe the visibility of interface methods

A

All methods declared in an interface must be public; this is the nature of an interface.

66
Q

How do you implement an interface?

A

With the ‘implements’ operator. All methods in the interface must be implemented within a class, with the exact same method signatures, or a fatal error will result.

67
Q

Can classes implement more than one interface?

A

Yes, by separating each interface with a comma.

68
Q

What happens if a class implements two interfaces that specify a method with the same name?

A

PHP allows this, as long as the duplicate methods have the same signature.

69
Q

Can interfaces be extended, like classes?

A

Yes, with the ‘extends’ operator.

70
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.

71
Q

How would you print an interface constant?

A

Where ‘a’ is the interface name and ‘b’ is the constant name – echo a::b;

72
Q

What are traits?

A

Traits are a mechanism for code reuse in single inheritance languages like PHP. They enable the application of class members without requiring inheritance. They’re essentially language-assisted copy and paste.

73
Q

Can you instantiate a trait on its own?

A

No.

74
Q

Describe the precedence order in traits.

A

Members from the current class override trait methods, which in turn override inherited methods.

75
Q

How do you insert a trait?

A

With the ‘use’ keyword.

76
Q

Can multiple traits be inserted into a class?

A

Yes, by listing them in the ‘use’ statement, separated by commas.

77
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.

78
Q

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

A

With the ‘insteadof’ operator. However, since you can only exclude methods with the insteadof operator, the ‘as’ operator can be used to allow the inclusion of one of the conflicting methods under another name.

79
Q

How can you adjust the visibility of a trait’s method in the exhibiting class?

A
// Change visibility of sayHello
class MyClass1 {
    use HelloWorld { sayHello as protected; }
}
// Alias method with changed visibility
// sayHello visibility not changed
class MyClass2 {
    use HelloWorld { sayHello as private myPrivateHello; }
}
80
Q

Can traits themselves make use of traits?

A

Yes. A trait can be composed partially or entirely from members defined in other traits.

81
Q

How can a trait impose a requirement on an exhibiting class?

A

The trait can specify an abstract method.

82
Q

Can traits define static members and methods?

A

Yes.

83
Q

Can traits define properties?

A

Yes.

84
Q

What happens if a trait defines a property and then a class defines a property with the same name?

A

An error is issued. If the class definition is compatible (same visibility and initial value), it’s an E_STRICT. Otherwise, fatal error.

85
Q

Unlike inheritance, if a trait has static properties…

A

…each class using that trait has independent instances of those properties.

86
Q

Do the ‘use’ operator for namespaces and the ‘use’ operator for traits resolve names the same?

A

No. ‘use’ for namespaces always sees its arguments as absolute, starting at the global namespace. ‘use’ for traits respects the current namespace.

87
Q

What are the three different ‘use’ operators for?

A

Traits, namespaces, and closures.

88
Q

What does ‘overloading’ in PHP mean?

A

It 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.

89
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.

90
Q

What is the visibility of overloading methods?

A

All overloading methods must be defined as public.

91
Q

Can arguments to overloading methods be passed by reference?

A

No.

92
Q

What are the property overloading methods?

A

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

93
Q

When are each of the property overloading methods run?

A

__set() is run when writing data to inaccessible properties.
__get() is used for reading data from inaccessible properties.
__isset() is triggered by calling isset() or empty() on inaccessible properties.
__unset() is invoked when unset() is used on inaccessible properties.

94
Q

What arguments do the property overloading methods take?

A

Each takes a string that is the name of the property being interacted with. __set() also takes a value which specifies the value that the named property should be set to.

95
Q

Property overloading only works in ______ context.

A

Object context.

96
Q

Are magic methods triggered in static context?

A

No, only in object context.

97
Q

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

A

A warning is issued.

98
Q

The return value of set is…

A

…ignored.

99
Q

When is __get() never called?

A

When chaining assignments together like this:

$a = $obj->b = 8;

100
Q

What are the method overloading functions, and when are they called?

A

__call(): triggered when invoking inaccessible methods in an object context.
__callStatic(): triggered when invoking inaccessible methods in a static context.

101
Q

What are the arguments to the method overloading functions?

A

Both take a string and an array. The string is the name of the method being called. The array is an enumerated array containing the parameters passed to the named method.

102
Q

What are three different ways to iterate over an object?

A
  1. $class = new Obj; foreach($class as $key => $value) { print “$key => $value\n”; } – That would print out all of the visible properties.
  2. Do the same as #1, but called from an object method iterateVisible() within the class. This would print out all of the public properties and the protected/private ones too.
  3. The Iterator interface.
103
Q

What is the IteratorAggregate interface?

A

It’s an alternative to implementing all of the Iterator methods. It only requires the implementation of a single method, IteratorAggregator::getIterator(), which should return an instance of a class implementing Iterator.

104
Q

Name the 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.

105
Q

Why shouldn’t you start function names with “__”?

A

Because PHP reserves all function names starting with __ as magical.

106
Q

What are is __sleep() for?

A

When you use serialize(), it checks if your class has a __sleep() method, and executes it prior to any serialization. It returns 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.

107
Q

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

A

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

108
Q

What is the intended use of __sleep()?

A

To commit pending data or perform similar cleanup tasks.

109
Q

What is __wakeup() for?

A

When you use unserialize(), it 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.

110
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.

111
Q

What is __toString()?

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” would print.

112
Q

What should __toString() return?

A

It MUST return a string. If not, a fatal E_RECOVERABLE level error is emitted.

113
Q

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

A

No. Doing so will result in a fatal error.

114
Q

What is the __invoke() method?

A

It’s called when a script tries to call an object as a function.

115
Q

What is the __set_state() method?

A

It’s a static method called for classes exported by var_export(). It takes as its argument an array containing exported properties.

116
Q

What is __debugInfo()?

A

It’s 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.

117
Q

What does the ‘final’ keyword do?

A

It prevents child classes from overriding a method.

118
Q

What happens when you define a class itself as final?

A

It cannot be extended. If it is, it causes a fatal error.

119
Q

If you specify a class as final, do you also have to specify each method as final?

A

No.

120
Q

Can properties be declared final?

A

No, only classes and methods.

121
Q

What are two situations where you would not want fully replicated properties when creating a copy of an object?

A
  1. You have an object which represents a GTK window, and the object holds the resource of this window. When you duplicate the object, you might want to create a new window with the same properties, and have the new object hold the resource of the window.
  2. If your object holds a reference to another object which it uses, and when you replicate the parent object you want to create a new instance of this other object so that the replica has its own separate copy.
122
Q

How do you copy an object?

A

With the ‘clone’ keyword. It calls an objects __clone() method if possible.

123
Q

Can an objects __clone() method be called directly?

A

No.

124
Q

When an object is cloned, PHP will perform a ____ copy of all the object’s ___.

A

When an object is cloned, PHP will perform a shallow copy of all the object’s properties.

125
Q

When an object is cloned, any properties that are references to other variables will ____ .

A

When an object is cloned, any properties that are references to other variables will remain references.

126
Q

When is an object’s __clone() method called?

A

After the cloning is complete.

127
Q

When are two objects equal? Identical?

A

Two objects are equal ( == ) when they have the same attributes and values, and are instances of the same class.

Two objects are identical ( === ) when they refer to the same instance of the same class.

128
Q

Can extensions define their own rules for object comparison ( == ) ?

A

Yes.

129
Q

Using type hinting, what can functions force parameters to be?

A

Objects, interfaces, arrays, or callable.

130
Q

How do type hint an object?

A

By specifying the name of the class in the function prototype.

131
Q

When type hinting in an object, what happens if NULL is used as the default parameter value?

A

It will be allowed as an argument for any later call.

132
Q

If a class or interface is specified as a type hint, then…

A

…all its children or implementations are allowed too.

133
Q

What types can NOT be used with type hints?

A

Scalar types such as int or string. Resources and traits are not allowed either.

134
Q

What happens when you fail to satisfy a type hint?

A

A catchable fatal error.

135
Q

Does type hinting work with functions?

A

Yes.

136
Q

What are late static bindings used for?

A

For referencing the called class in a context of static inheritance.

137
Q

Why are late static bindings called as such?

A

“Late binding” comes from the fact that static:: will not be resolved using the class where the method is defined but it will rather be computed using runtime information. “Static” is from the fact that it can be used for (but is not limited to) static method calls.

138
Q

Static references to the current class like self:: or __CLASS__ are resolved using…

A

…the class in which the function was defined.

139
Q

Can static:: refer to non-static properties?

A

No.

140
Q

As of PHP 5, an object variable doesn’t contain…

A

…the object itself as a value. It contains an object identifier which allows object accessors to find the actual object.

141
Q

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

A

…not aliases. They hold a copy of the identifier, which points to the same object.

142
Q

What does serialize() return?

A

A string containing a byte-stream representation of any value that can be stored in PHP.

143
Q

Using serialize to save an object will save…

A

…all variables in an object.

144
Q

When an object is serialized, are the methods saved?

A

No, only the name of the class.

145
Q

In order to be able to unserialize an object…

A

…the class of that object must be defined.

146
Q

What happens if an object is unserialized without its class definition?

A

PHP gives the object a class of __PHP_Incomplete_Class_Name, which has no methods, and would render the object useless.