Classes and Objects Flashcards
What can a class name be?
Any valid label, provided it is not a PHP reserved word.
A valid class name starts with…
…a letter or underscore, followed by any number of letters, numbers, or underscores.
What can a class contain?
Its own constants, variables (called “properties”), and functions (called “methods”).
What is $this, and when is it available?
$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.
When will an object not be created when using the ‘new’ keyword?
When the object has a constructor defined that throws an exception on error.
Do classes need to be defined before instantiation?
It’s not a requirement, but they should be.
What happens when a string containing the name of a class is used with the ‘new’ keyword?
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 can you create a new object in the class context?
By using ‘new self’ and ‘new parent’.
When assigning an already-created instance of a class to a new variable, the new variable will access…
…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 can you make a copy of an already-created object?
You can clone it.
How can a class inherit the methods and properties of another class?
By using the ‘extends’ keyword in the class declaration.
How many levels of classes can you extend?
Just one; a class can only inherit from one base class.
When can a parent class’s method not be overridden?
When it was declared as ‘final’.
How can the overridden methods of a parent class be accessed?
By referencing them with parent::
What happens when you override a method, but use a different parameter signature?
PHP will generate an E_STRICT level error. This does not apply to the constructor, which allows overriding with different parameters.
How can you get a string containing the fully qualified name of the ClassName class?
ClassName::class
What is the difference between $this and self?
Inside a class definition, $this refers to the current object, while self refers to the current class.
What is stdClass?
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.
What are the three different names for class member variables?
Properties, attributes, and fields.
Is the keyword ‘var’ required in property declarations?
No, it’s deprecated.
Can property values in a class include an initialization:
$a = 1;
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.
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…
…public.
Can heredocs be used in property declarations?
No, but nowdocs can.
Can you define constant values on a per-class basis?
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.