card for PHP Advance level that shoul include examples too with definitions Flashcards
What is the purpose of namespaces in PHP?
Namespaces are used to encapsulate items such as classes, interfaces, functions, and constants to avoid name collisions.
True or False: PHP supports multiple inheritance.
False. PHP does not support multiple inheritance but allows interfaces to achieve similar functionality.
Fill in the blank: The PHP function _______ is used to include files.
include
What is the difference between ‘include’ and ‘require’ in PHP?
‘include’ generates a warning if the file cannot be found, while ‘require’ generates a fatal error.
What is a closure in PHP?
A closure is an anonymous function that can capture variables from its surrounding scope.
Provide an example of a closure in PHP.
$greet = function($name) { return ‘Hello, ‘ . $name; };
What does the ‘final’ keyword do in a class definition?
The ‘final’ keyword prevents a class from being inherited.
What is the purpose of the ‘static’ keyword in PHP?
The ‘static’ keyword is used to declare properties and methods that belong to the class itself rather than to any specific object instance.
True or False: Traits in PHP allow for code reuse in single inheritance.
True. Traits enable code reuse in classes that are not related through inheritance.
What is the purpose of the ‘abstract’ keyword in PHP?
The ‘abstract’ keyword is used to define a class that cannot be instantiated and may contain abstract methods that must be implemented in derived classes.
What is an interface in PHP?
An interface defines a contract that classes must follow, specifying methods that must be implemented without providing the implementation.
Provide an example of an interface in PHP.
interface Animal { public function makeSound(); }
What does the ‘instanceof’ operator do in PHP?
The ‘instanceof’ operator checks whether an object is an instance of a specific class or interface.
What is the purpose of the ‘try-catch’ block in PHP?
The ‘try-catch’ block is used for exception handling, allowing you to catch and handle exceptions that occur during execution.
True or False: PHP supports operator overloading.
False. PHP does not support operator overloading.
What is PDO in PHP?
PDO (PHP Data Objects) is a database access layer providing a uniform method of access to multiple databases.
What does the ‘bindParam’ method do in PDO?
‘bindParam’ binds a parameter to a specified variable name and allows for the execution of prepared statements.
What is the purpose of Composer in PHP?
Composer is a dependency manager for PHP, allowing you to manage libraries and packages for your projects.
Provide an example of how to install a package using Composer.
composer require vendor/package-name
What is a PSR in PHP?
PSR stands for PHP Standard Recommendation, which provides guidelines for coding standards and best practices in PHP.
What is the purpose of the ‘autoload’ feature in Composer?
The ‘autoload’ feature automatically loads classes without requiring manual ‘include’ or ‘require’ statements.
What does the ‘return’ statement do in a function?
The ‘return’ statement ends the function execution and returns a value to the calling code.
What are magic methods in PHP?
Magic methods are special methods that start with double underscores (__), allowing you to define behavior for object instantiation, property access, and more.
Provide an example of a magic method in PHP.
public function __construct() { // initialization code }
What is the purpose of the ‘serialize’ function in PHP?
The ‘serialize’ function converts a PHP value into a storable representation, typically for saving to a file or database.
What does the ‘json_encode’ function do?
The ‘json_encode’ function converts a PHP value into a JSON format string.