PHP Multiple Choice Questions on “Basics of Object-Oriented PHP”. 1. The practice of separating the user from the true inner workings of an application through well-known interfaces is known as _________ 2. Which of the following term originates from the Greek language that means “having multiple forms,” defines OOP’s ability to redefine, a class’s characteristics? 3. The practice of creating objects based on predefined classes is often referred to as ______________ 4. Which one of the following property scopes is not supported by PHP? 5. Which one of the following can be used to instantiate an object in PHP assuming class name to be Foo? 6. Which one of the following is the right way to define a constant? 7. Which one of the following is the right way to call a class constant, given that the class is mathFunction? 8. Which one of the following is the right way to invoke a method? 9. Which of the following is/are the right way to declare a method? a] Only ii] 10. Which of the following method scopes is/are not
supported by PHP? a] Only ii]
a] Polymorphism
b]
Inheritance
c] Encapsulation
d] Abstraction
Answer: c
Clarification: In object-oriented PHP encapsulation is a concept of wrapping up or binding up the data members and methods in a single module.
a] Abstraction
b] Polymorphism
c] Inheritance
d]
Differential
Answer: b
Clarification: The word polymorphism is derived from Greek word poly which means “many” and morphism which means the property which helps us to assign more than one property.
a] class creation
b] object creation
c] object instantiation
d] class instantiation
Answer:
d
Clarification: In object-oriented programming, classes are the blueprints of php objects. Classes do not actually become objects until instantiation is done. When someone instantiates a class, it creates an instance of it, thus creating the object. In other words, instantiation is the process of creating an instance of an object in memory.
a] friendly
b] final
c] public
d] static
Answer: a
Clarification: PHP supports five class property scopes: public, private, protected, final and static.
a] $obj = new $foo;
b] $obj = new foo;
c] $obj = new foo [];
d] obj = new foo [];
Answer: c
Clarification: To create a new object in PHP we can use the new statement to instantiate a
class.
a] constant PI = “3.1415”;
b] const $PI = “3.1415”;
c] constant PI = ‘3.1415’;
d] const PI = ‘3.1415’;
Answer: d
Clarification: Class constants are created like: const NAME = ‘VALUE’;
a] echo PI;
b] echo mathFunction->PI;
c]
echo mathFunction::PI;
d] echo mathFunction=PI;
Answer: c
Clarification: The Scope Resolution Operator “::” is a token that allows access to static, constant, and overridden properties or methods of a class.
a] $object->methodName[];
b] object->methodName[];
c] object::methodName[];
d] $object::methodName[];
Answer:
a
Clarification: “->” is a dynamic class method invocation in PHP.i] function functionName[] { function body }
ii] scope function functionName[] { function body }
iii] method methodName[] { method body }
iv] scope method methodName[] { method body }
b] Only iv]
c] i] and ii]
d] iii] and iv]
Answer: c
Clarification: In case of public methods, you can forgo explicitly declaring the scope and just declare the method like you would a function.i] private
ii] friendly
iii] static
iv] abstract
b] Only iv]
c] ii] and iv]
d] Only i]
Answer: a
Clarification: PHP supports six method scopes: public, private, final, static, protected and abstract. But it does not support friendly.
We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
Class member variables are called properties. They may be referred to using other terms such as fields, but for the purposes of this reference properties will be used. They are defined by using at least one modifier [such as Visibility, Static
Keyword, or, as of PHP 8.1.0, readonly], optionally [except for readonly
properties], as of PHP 7.4, followed by a type declaration, followed by a normal variable declaration. This declaration may include an initialization, but this initialization must be a constant value.
Note:
An obsolete way of declaring class properties, is by using the
var
keyword instead of a modifier.
Note: A property declared without a Visibility modifier will be declared as
public
.
Within class methods non-static properties may be accessed by using ->
[Object
Operator]: $this->property [where property
is the name of the property]. Static properties are accessed by using the ::
[Double Colon]: self::$property. See Static Keyword for more information on the difference between static and non-static properties.
The pseudo-variable $this is available inside any class method when that method is called from within an object context. $this is the value of the calling object.
Example #1 Property declarations
Note:
There are various functions to handle classes and objects. See the Class/Object Functions reference.
Type declarations
As of PHP 7.4.0, property definitions can include Type declarations, with the exception of callable.
Example #2 Example of typed properties
The above example will output:
Typed properties must be initialized before accessing, otherwise an Error is thrown.
Example #3 Accessing properties
The above example will output:
string[8] "triangle" int[3] string[6] "circle" Fatal error: Uncaught Error: Typed property Shape::$numberOfSides must not be accessed before initialization
Readonly properties
As of PHP 8.1.0, a property can be declared with the readonly
modifier, which prevents modification of the
property after initialization.
Example #4 Example of readonly properties
Note:
The readonly modifier can only be applied to typed properties. A readonly property without type constraints can be created using the mixed type.
Note:
Readonly static properties are not supported.
A readonly property can only be initialized once, and only from the scope where it has been declared. Any other assignment or modification of the property will result in an Error exception.
Example #5 Illegal initialization of readonly properties
Note:
Specifying an explicit default value on readonly properties is not allowed, because a readonly property with a default value is essentially the same as a constant, and thus not particularly useful.
Note:
Readonly properties cannot be unset[] once they are initialized. However, it is possible to unset a readonly property prior to initialization, from the scope where the property has been declared.
Modifications are not necessarily plain assignments, all of the following will also result in an Error exception:
However, readonly properties do not preclude interior mutability. Objects [or resources] stored in readonly properties may still be modified internally:
Anonymous ¶
10 years ago
In case this saves anyone any time, I spent ages working out why the following didn't work:
class MyClass
{
private $foo = FALSE;
public function __construct[]
{
$this->$foo = TRUE;
echo[$this->$foo];
}
}
$bar = new MyClass[];
giving "Fatal error: Cannot access empty property in ...test_class.php on line 8"
The subtle change of removing the $ before accesses of $foo fixes this:
class MyClass
{
private $foo = FALSE;
public function __construct[]
{
$this->foo = TRUE;
echo[$this->foo];
}
}
$bar = new MyClass[];
I guess because it's treating $foo like a variable in the first example, so trying to call $this->FALSE [or something along those lines] which makes no sense. It's obvious once you've realised, but there aren't any examples of accessing on this page that show that.
anca at techliminal dot com ¶
7 years ago
You can access property names with dashes in them [for example, because you converted an XML file to an object] in the following way:
Anonymous ¶
11 years ago
$this can be cast to array. But when doing so, it prefixes the property names/new array keys with certain data depending on the property classification. Public property names are not changed. Protected properties are prefixed with a space-padded '*'. Private properties are prefixed with the space-padded class name...
To get an array with all property names unaltered, use the 'get_object_vars[$this]' function in any method within class scope to retrieve an array of all properties regardless of external visibility, or 'get_object_vars[$object]' outside class scope to retrieve an array of only public properties [see: PHP manual page].
This is documented behavior when converting any object to an array [see PHP manual page]. All properties regardless of visibility will be shown when casting an object to array [with exceptions of a few built-in objects].
zzzzBov ¶
12 years ago
Do not confuse php's version of properties with properties in other languages [C++ for example]. In php, properties are the same as attributes, simple variables without functionality. They should be called attributes, not properties.
Properties have implicit accessor and mutator functionality. I've created an abstract class that allows implicit property functionality.
after extending this class, you can create accessors and mutators that will be called automagically, using php's magic methods, when the corresponding property is accessed.
kchlin dot lxy at gmail dot com ¶
3 days ago
From PHP 8.1
It's easy to create DTO object with readonly properties and promoting constructor
which easy to pack into a compact string and covert back to a object.