Difference between revisions of "PHP interview"

From Hawk Wiki
Jump to: navigation, search
(Protected)
(Magic Methods)
Line 99: Line 99:
 
</pre>
 
</pre>
  
==Magic Methods==
+
==Advanced==
 +
===Magic Methods===
 
<pre class="php">
 
<pre class="php">
 
<?php
 
<?php

Revision as of 23:43, 3 April 2012

Basic

Protected

A protected access specifier is mainly used with inheritance. A data member or member function declared as protected will be accessed by its class and its base class but not from the outside world (i.e. rest of the script). We can also say that a protected data member is public for the class that declares it and it’s child class; but is private for the rest of the program (outside world).

abstract class

Can not be Instantiable.(cannot become instant. Can only be inherited)

abstract class Fruit {
 private $color;

 abstract public function eat()

 public function setColor($c) {
  $this->color = $c;
 }
}

Difference between Abstract Class and Interface
Abstract Classes

An abstract class can provide some functionality and leave the rest for derived class
The derived class may or may not override the concrete functions defined in base class
The child class extended from an abstract class should logically be related

Interface

An interface cannot contain any functionality. It only contains definitions of the methods
The derived class must provide code for all the methods defined in the interface
Completely different and non-related classes can be logically be grouped together using an interface

Single or Double Quotes?

//Sometimes people use double quotes in PHP to avoid having to use the period to separate code. For example, you could write:
$color='red';
echo "I have a $color shirt on today<br/>";
//Faster however is not always better. A better way to write this code would be:
echo 'I have a ' .$color. ' shirt on today<br/>';
echo 'I have a $color shirt on today<br/>';

Output:

I have a red shirt on today
I have a red shirt on today
I have a $color shirt on today

Avoid Mysql Injection

$_POST['username'] = 'aidan';
$_POST['password'] = "' OR ''='";
$query = "SELECT * FROM users WHERE user='{$_POST['username']}' AND password='{$_POST['password']}'";
$passwd=mysql_real_escape_string($_POST['password']);
$passwd1=addslashes($_POST['password']);
echo $passwd."<br />";
echo $passwd1."<br />";
echo ($query);

Output

\' OR \'\'=\'
\' OR \'\'=\'
SELECT * FROM users WHERE user='aidan' AND password='' OR ''=''

This would allow anyone to log in without a valid password.

Static method

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class.

<?php
class Foo
{
    public static $my_static = 'foo';

    public function staticValue() {
        return self::$my_static;
    }
}

class Bar extends Foo
{
    public function fooStatic() {
        return parent::$my_static;
    }
}


print Foo::$my_static . "\n";

$foo = new Foo();
print $foo->staticValue() . "\n";
print $foo->my_static . "\n";      // Undefined "Property" my_static 
print $foo::$my_static . "\n";
$classname = 'Foo';
print $classname::$my_static . "\n"; // As of PHP 5.3.0

print Bar::$my_static . "\n";
$bar = new Bar();
print $bar->fooStatic() . "\n";

Advanced

Magic Methods

<?php

class Customer {
private $firstName, $lastName, $email;

public function __construct($firstName, $lastName, $email) {
$this->firstName = $firstName;
$this->lastName = $lastName;
$this->email = $email;
}

public function __toString() {
return “Debug message from Customer Class : First Name = ” . $this->firstName . “, Last Name = ” . $this->lastName . “, Email = ” . $this->email;
}
}

$c = new Customer(“Sunil”,”Bhatia”,”email@domain.com”);

echo “Customer Object is >>” . $c;

?>