1. What is magic function in PHP?
PHP functions with __ within the class called is a magic function. It can be callled indirectly. Sample magic functions are __construct(), __destruct(), __call(), __callStatic(), __get(), __set(), __isset(), __unset(), __sleep(), __wakeup(), __toString(), __invoke(), __set_state(), __clone(), and __autoload().
If you want to create a object for class, it automatically call __construct indirectly.
2. what is the __autoload function? Can you provide an example of how it’s used?
It is one of the magic function because it starts with __ however it will be used to load the class file automatically. Lets take below example without autoload function
include_once "ClassA.php";
include_once "ClassB.php";
include_once "ClassC.php";
$a = new ClassA();
$b = new ClassB();
$c = new ClassC();
We have to make include statement for each class file before initiate the object. Lets think about if have 50 classes and needs to be initiated in the php program. We have to make
50 lines of include_once or required_once then use the class. autoload magic function brings the simplicity here by detecting the necessary classes while creating object. Using this we include the class file automatically. Lets take example with autoload function
function __autoload($class_name)
{
require_once $class_name;
}
$a = new ClassA();
$b = new ClassB();
$c = new ClassC();
3. When is the __autoload function called?
The __autoload function gets called anytime when unknown class reference arised. Pre-requisite is that magic function should be defined inside the class.
4. Does the __autoload function works for static function calls?
Yes. because static function gets called using class reference.
5. When else the __autoload function called?
When invoking class_exists api this gets called however there is an additional parameter where we disable this.
6. Difference between self and $this?
$this - refers current object
self - refers current class
Example
Class A
{
public function whichClass()
{
echo "I am A";
}
public function sayClassName()
{
$this->sayClassName();
}
}
class B extends A
{
public function whichClass()
{
echo "I am B";
}
}
$obj = new B();
$obj->sayClassName();
Output "I am B"
Change $this to self:: then check output "I am A"
7. What is vtable?
8. What is the difference between == and ===?
== checks left and right value are equal.
=== checks left and right value are equal also checks variable type same
9. Is late static binding possible?
Not possible before PHP 5.3
10. Difference between unset vs array_slice?
unset delete the element in the array and it will not reorder the index whereas array_slice does.
11. Array_slice and array_splice?
$input = ["white", "yellow", "blue", "orange"];
$res = array_slice($input,2);
Result
array_slice return you the part which offset you requested, third parameter is optional to specify the length of tokens.
// $res = [0 => "blue", 1 => "orange"]; here res is a new array which starts with 0 index
Array_splice
Remove a portion of the array and replace it with something else
12. How to set infinite time execution for php script?
set_time_limit(0) which runs php script without time limitation.
13. What is the difference between mysqli_fetch_object and mysqli_fetch_array()?
mysql_fetch_object collects first single row whereas mysqli_fetch_array collects all the rows.
14. How is it possible to remove escape characters from a string?
stripslashes() method used to remove the escape characters.
15. Is it possible to remove tags from the data?
stripe_tags() method used to remove the tags from the data.