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.
Can a class be referenced using a variable?
Yes, but the variable’s value can not be a keyword.
What is the visibility of class constants?
They are always publicly visible; they can’t be made private or protected.
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 (PHP would otherwise fail with an error).
Why is using __autoload() discouraged?
It may be deprecated or removed in the future. spl_autoload_register() provides a more flexible alternative for autoloading classes.
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.
When is Autoloading not available?
When using PHP in CLI interactive mode.
If a child class defines a constructor, is a parent class called implicitly?
No. It must be explicitly referenced with a call to parrent::__construct(), within the child constructor.
If a child class does not define a constructor, can it be inherited from the parent class
Yes, as long as it was not declared private.
What will PHP do if it cannot find a __construct function for a given class, and the class did not inherit a parent one?
It will search for the old-style constructor function, by the name of the class.
When happens __construct() is overridden with different parameters than the parent __construct() method has?
PHP will NOT generate an E_STRICT level error message.
As of PHP 5.3.3, methods with the same name as the last element of a namespaced class name…
… will no longer be treated as constructor. This change doesn’t affect non-namespaced classes.
What is __destruct()?
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.
Are parent destructors implicitly called by the engine?
No; you have to explicitly call parent::__destruct() in the destructor body.
Can destructors be inherited from a parent class?
Yes, they can, just like constructors.
If script execution is stopped with exit(), are destructors still called.
Yes. However, calling exit() from within a destructor will prevent the remaining shutdown routines from executing.
Can you throw an exception from a destructor?
No; it causes a fatal error (when called in the time of script termination).
How can the visibility of a property or method be defined?
By prefixing the declaration with the keywords public, protected, or private.
What are the definitions of the visibility keywords?
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.
Must a visibility keyword be used to define a class property?
Yes. If declared using ‘var’, the property will be defined as public.
Must a visibility keyword be used to define a class method?
No. Methods without any explicit visibility keyword are defined as public.
Do objects of the same type have access to each others’ private and protected members even though they are not the same instances?
Yes they do.
Can classes be used before they are defined?
Not unless autoloading is used.
If a class extends another, can the parent class be declared after the child class structure?
No. This rule applies to classes that inherit other classes and interfaces.
How can you force a class to be strictly an inheritable class?
Use the ‘abstract’ keyword. Then any attempt to instantiate the class directly will result in a fatal error.
What is the scope operator also called?
Paamayim Nekudotayim
Can a class be referenced using a variable?
Yes, but that variable cannot be a keyword (self, static, parent).
Can a class constant, static class property, and static class function all share the same name and be accessed with the scope operator?
Yes.
What are the three uses of the static keyword?
- Defining static methods and properties.
- Defining static variables.
- Late static bindings.
What is the advantage of declaring class properties or methods as static?
It makes them accessible without needing an instantiation of their class.
Can a property declared as static be accessed with an instantiated class object?
No, but a static method can.
Is the pseudo-variable $this available inside a method declared as static?
No.
Can static properties be accessed using the arrow operator: -> ?
No.
What happens when you call non-static methods statically?
It generates an E_STRICT level warning.
Can static properties be initialized with an expression?
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.