Classes and Objects Flashcards
(96 cards)
What is a valid class name?
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, and functions (called “methods”).
What is the pseudo-variable $this?
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 the new keyword not create an instance of a class?
When the object has a constructor defined that throws an exception on error.
When a class is in a namespace, how can a new instance of that class be created?
Using its fully qualified name.
In the class context, it is possible to create a new object by…
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.
What is the extends keyword?
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 can you override inherited methods and properties/
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::.
When overriding methods, the parameter signature should…
…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.
You can get a string containing the fully qualified name of the ClassName class by using…
…ClassName::class
What are properties?
Class member variables. They are also referred to as “attributes” and “fields”.
How are properties defined?
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.
What happens if you declare a property using var instead of one of public/private/protected?
PHP 5 will treat the property as if it had been declared as public.
Can heredocs be used in a static data context, including property declarations?
No, but nowdocs can.
What are the limits on defining constants on a per-class basis?
The value must be a constant expression, not (for example) a variable, a property, a result of a mathematical operation, or a function call.
Can you reference a class using a variable?
Yes, but the variable’s value can not be a keyword (e.g. self, parent and static).
What is an __autoload() function?
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.
Why is using __autoload() discouraged?
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.
Can exceptions thrown in the __autoload() function be caught in the catch block?
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.
Is autoloading available if using PHP in CLI interactive mode?
No.
What is a constructor method?
A method which is called on each newly-created object.
How can you run a parent constructor?
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).
Are methods with the same name as the last element of a namespaced class name treated as constructor?
No, not any longer.