PHP ZCE mock test, interview preparation, daily lessons under chalk talk

Friday, January 20, 2012

PHP instanceOf and Type Hinting usage

instanceOf: It is one of the type operator present in php.


- It is used to determine whether a PHP variable is an instantiated object of a particular class.


- It can also be used to determine whether a variable is inheriting properly from another class. 


- Also used to determine whether a variable is an instantiated object of a class that implements an interface.




Example:


<?php
class ParentClass
{
}

class MyClass extends ParentClass
{
}

$a = new MyClass;

var_dump($a instanceof MyClass);
var_dump($a instanceof ParentClass);
?>

Output:

bool(true)
bool(true)




Type Hinting: It is a feature that is added in PHP5


- It allows functions to force parameters to be objects by specifying the name of the class in the function prototype.


- It allows to use NULL as default parameter value.


Example:


    //First parameter must be an object of type OtherClass
     public function test(OtherClass $otherclass)

    //First parameter must be an array    public function test_array(array $input_array) 


Question 1:



When checking to see if two variables contain the same instance of an object, which of the following comparisons should be used?
 1)    if($obj1->equals($obj2) && ($obj1 instanceof $obj2))
 2)    if($obj1->equals($obj2))
 3)    if($obj1 === $obj2)
 4)    if($obj1 instanceof $obj2)
 5)    if($obj1 == $obj2)




Question 2:



In PHP 5 you can use the ______ operator to ensure that an object is of a particular type. You can also use _______ in the function declaration.
1)     instanceof, is_a
2)     instanceof, type-hinting
3)     type, instanceof
4)     ===, type-hinting
5)     ===, is_a


Solution 1:
3)     if($obj1 === $obj2)


Solution 2:
2)     instanceof, type-hinting

Thursday, January 12, 2012

ZCE Zend Certified Engineer String return problem


View the Complete Blog on ZCE

function stringAppend(&$str)

{

        return $str = $str."append";

}

function stringPrepend(&$str)

{

         $str = "prepend".$str;

}


$aString = "PHP";

echo stringPrepend(stringAppend($aString));

Answer: NULL
becuase stringPrepend dint return anything


ZCE Zend Certified Engineer type hinting problem

View the Complete Blog on ZCE

What is the best way to ensure that a user-defined function is always passed an object as its single parameter?



1. 
2. function myfunciton($a = stdClass)
3. Use is_object() within the function
4. There is no way to ensure the parameter will be an object
5. function myfunction(Object $a)


Answer: 1.

ZCE Zend Certified Engineer Counter Problem


View the Complete Blog on ZCE

<?php

$string = "111221";

for($i = 0; $i < strlen($string); $i++) {
 
 $current = $string[$i];
 $count = 1;
 
 while(isset($string[$i + $count]) && ($string[$i + $count] == $current)) $count++;
 
 $newstring .= "$count{$current}";
 
 $i += $count-1;
}

print $newstring;
?>

Answer: 312211

ZCE Zend Certified Engineer Number Format Problem


View the Complete Blog on ZCE

<?php

$a = 1;
$b = 2.5;
$c = 0xFF;

$d = $b + $c;
$e = $d * $b;
$f = ($d + $e) % $a;

print ($f + $e);

?>
Solution:
$c = 0xFF => 255
$d = $b+$c => 257.5
$e = $d * $b => 643.75
$f = ($d+$e) % $a => ($d+$e) % 1 =>0
therefore answer is 643.75

ZCE Zend Certified Engineer Define problem


View the Complete Blog on ZCE

define('FOO', 10);

$array = array(10 => FOO,
               "FOO" => 20);

print $array[$array[FOO]] * $array["FOO"];
Answer: $array[FOO] will be replaced by $array[10].
The value of $array[10] is again FOO ie 10
therefore $array[$array[FOO]] = 10


ZCE Zend Certified Engineer Valid Variables

View the Complete Blog on ZCE


Which of the following are valid PHP variables?


1. @$foo
2. &$variable
3. ${0x0}
4. $variable
5. $0x0




Solution: 1,2,3,4
1. @ is used to suppress the error and is completely valid to use in front of a variable.
2. & is a reference variable
3. {} ie the Curly braces are used inside the double quotes in order to avoid variable names conflict. 
4. We all know, its valid
5. Variable name can not begin with a number.





ZCE Zend Certified Engineer Nested Try Catch Problem Solution

View the Complete Blog on ZCE


class MyException extends Exception { }
class Test {
        public function testing()
        {
                try
                {
                        try
                        {
                                throw new MyException('foo!');
                        }
                        catch (Exception $e)
                        {
                                echo 'cought1';
                                /* rethrow it */
                                throw $e;
                        }
                        catch (MyException $e)
                        {
                                echo 'cought2';
                                /* rethrow it */
                                throw $e;
                        }
                }
                catch (MyException $e)
                {
                        echo 'cought3';
                        echo $e->getMessage();
                }
        }
}

$foo = new Test;
$foo->testing();

Answer: cought1cought3foo!
Thrown exception will be first caught by 'caught1' because Exception class that is the generic Handler for all sort of exceptions. Now as it rethrows the exception, it will be caught by Outer Catch Block.

ZCE Zend Certified Engineer strrpos question solution

View the Complete Blog on ZCE


$foo = 'charanjeet/working/with/php';
echo strrpos($foo, '/', -5);

Answer: 18
It will start searching it reverse direction after ignoring 5 five characters.
It will then return the position of the / after working, from the beginning

ZCE Zend Certified Engineer - Number update problem

View the Complete Blog on ZCE


function updateValue(&$number)
{
$number = $number +1;
}
$val = 9;
echo updateValue(&$val);

Answer: It will also return blank
becuase despite updating $number is not returned from the function

Wednesday, January 11, 2012

ZCE Zend Certified Engineer String Append Prepend question answer

View the Complete Blog on ZCE


function stringAppend($str)
{
$str = $str."append";
}
function stringPrepend($str)
{
$str = "prepend".$str;
}

$aString = "PHP";
echo stringPrepend(stringAppend($aString));


Answer: NULL
Because aString is not passed by reference.