Example #1
0
class ABC
{
    private $array = ['username' => 'scott', 'password' => 'p@$$word', 'isActive' => true];
    private $user = '******';
    public function getUser()
    {
        return $this->user;
    }
    /* create __call function */
    /* name is string & parameter is array*/
    public function __call($function_name, $function_parameter)
    {
        return "{$function_name}" . implode(', ', $function_parameter) . "\n";
        /*if(array_key_exists($function_name,$this->array)){
        			return $this->array[$function_name];
        		}else{
        			echo "You are trying to call a function name $function_name with following parameter:";			
        			print_r($function_parameter);
        		}*/
    }
    private function getUserName($var)
    {
        return 'sdfs' . $var;
    }
}
$obj = new ABC();
/* call member function. */
//echo $obj->getUser();
/* Fatal error: Call to undefined method ABC::getUserName() */
echo $obj->getUserName('abc');
//echo $obj->isActive()?'Logged In':'Not Logged In';
Example #2
0
            }
        }
    }
    public function __get($name)
    {
        if (property_exists($this, $name)) {
            return $this->{$name};
        }
    }
    public function results()
    {
        return $this->array;
    }
}
/* create object */
$obj = new ABC();
/* set the value/ assign value 
$obj->parray =array('asd','xyz');
var_dump($obj->parray);
*/
$obj->text = 'abc';
$obj->key = "!@#\$%^&";
echo $obj->key;
var_dump($obj->results());
//var_dump($obj->array);
/* reference */
/*
@this is slower (than getters/setters)
@there is no auto-completion (and this is a major problem actually), and type management by the IDE for refactoring and code-browsing (under Zend Studio/PhpStorm this can be handled with the @property phpdoc annotation but that requires to maintain them: quite a pain)
@the documentation (phpdoc) doesn't match how your code is supposed to be used, and looking at your class doesn't bring much answers as well. This is confusing.
@added after edit: having getters for properties is more consistent with "real" methods where getXXX() is not only returning a private property but doing real logic. You have the same naming. For example you have $user->getName() (returns private property) and $user->getToken($key) (computed). The day your getter gets more than a getter and needs to do some logic, everything is still consistent.
Example #3
0
<?php

class ABC
{
    public function __construct()
    {
        echo "inside construct";
    }
    public function __destruct()
    {
        echo "inside destruct";
    }
    public function xyz()
    {
        echo "inside xyz";
    }
    public function s()
    {
        echo "inside s";
    }
}
$a = new ABC();
$a->xyz();
$a->s();
Example #4
0
<?php

/*
* @ __callstatic	
* @description
*  call static is same as call function. using call function we call object of the functions.
*  but in __callstatic we call context of the function.
*/
class ABC
{
    private $array = ['abc' => 'ABC variable', 'xyz' => 'XYZ variable'];
    public static function __callstatic($name, $value)
    {
        if (function_exists($name)) {
            return 'function exists';
        } else {
            echo 'function not exists';
        }
    }
    public static function getName()
    {
        return "SCott";
    }
}
var_dump(ABC::getName());
Example #5
0
<?php

trait A
{
    function p()
    {
        echo "A\n";
    }
}
trait B
{
    function p()
    {
        echo "B\n";
    }
}
trait C
{
    function p()
    {
        echo "C\n";
    }
}
class ABC
{
    use A, B, C {
        A::p insteadof B, C;
    }
}
$abc = new ABC();
$abc->p();