Tuesday, August 3, 2010

__set() & __get() Magic Methods

In the below example class Foo uses set and get methods while class Bar uses the magic methods. To summarise set and get, basically if you are trying to access a property of an object (and it has a get method) the name of the property you are requesting is passed into the get method and it can handle it how it wants. set works the same but it is only called when you attempt to assign a value to a member variable. The only difference being set takes two paramemters, the variable its setting and the value. 




class Foo{

    private $name;
    private $age;

    public function setName($name){
        $this->name = $name;
    }

    public function getName(){
        return $this->name;
    }

    public function setAge($age){
        $this->age = $age;
    }

    public function getAge(){
        return $this->age;
    }

}

$foo = new Foo();
$foo->setName("Dougal");
$foo->setAge(10);

echo $foo->getName(). " is " . $foo->getAge(). " years old\n";

class Bar{

    private $name;
    private $age;

    public function __set($var, $val){
        $this->$var = $val;
    }

    public function __get($var){
        return $this->$var;
    }

}
$bar = new Bar();
$bar->name = "Dougal";
$bar->age = 10;

echo $bar->name . " is " . $bar->age. " years old";
?>

Ajax Cache-Problem with Internet Explorer.

Include those lines into php file.

header('Pragma: no-cache');
header('Cache-Control: no-cache');
header('Expires: 0');

Word Wrap in php

$text "A very long woooooooooooord.";$newtext wordwrap($text8"\n"true);

echo 
"$newtext\n";?>

PHP Scripting in Command line

1. How can I execute a PHP script using command line?
Ans : “php filename” or “php -f filename” will run a php file in command line.

ZEND BLOG

http://shiflett.org/blog/2005/apr/zend-certification-self-test

ZEND TEST BOOK

http://www.boysj.com/the-zend-php-certification-practice-test-book

PHP CLASSES AND IF/ELSE BLOCK

//*********************************working
$flag='3';

if($flag=='3')
{
class cc {
function __construct() {
echo 'cc!';

}
}

}

$type = 'cc';
$obj = new $type;
//****************************************not working

$type = 'cc';
$obj = new $type;
$flag='3';

if($flag=='3')
{
class cc {
function __construct() {
echo 'cc!';

}
}

}


?>

what is interface in php? how it is use?


An Interface is like a template similar to abstract class
with a difference where it uses only abstract methods.

In simple words, an interface is like a class using
interface keyword and contains only function
declarations(function with no body).

An Interface should be implemented in the class and all the
methods or functions should be overwridden in this class.

for eg: 

interface InterfaceName{
  function fun1();
  function fun2(a,b);    
}

class ClassName implements InterfaceName{
  fuction fun1(){
    function implementation.......
  }
  fuction fun2(a,b){
    function implementation.......
  }
}