Htaccess    html   Php    javascript   Asp   css    maths  Past Questions  Practice Tests Online

language.oop5.overloading function syntax tag tutorial 2013 Donate at flattr Flattr this





Alert! Connect with 10,000 Chating Online. Join Now

Php language.oop5.overloading () function

Write your php code

 
<?php ?>

Php Result


Your code below



Overloading

Both method calls and member accesses can be overloaded via the __call, __get and __set methods. These methods will only be triggered when your object or inherited object doesn't contain the member or method you're trying to access.

Member overloading

void __set ( string name, mixed value )

mixed __get ( mixed name )

Class members can be overloaded to run custom code defined in your class by defining these specially named methods. The $name parameter used is the name of the variable that should be set or retrieved. The __set() method's $value parameter specifies the value that the object should set set the $name.

Example 19-18. overloading with __get and __set example

<?php
class Setter
{
    
public $n;
    
private $x = array("a" => 1, "b" => 2, "c" => 3);

    function
__get($nm)
    {
        print
"Getting [$nm]\n";

        if (isset(
$this->x[$nm])) {
            
$r = $this->x[$nm];
            print
"Returning: $r\n";
            return
$r;
        } else {
            echo
"Nothing!\n";
        }
    }

    function
__set($nm, $val)
    {
        print
"Setting [$nm] to $val\n";

        if (isset(
$this->x[$nm])) {
            
$this->x[$nm] = $val;
            echo
"OK!\n";
        } else {
            echo
"Not OK!\n";
        }
    }
}

$foo = new Setter();
$foo->n = 1;
$foo->a = 100;
$foo->a++;
$foo->z++;
var_dump($foo);
?>

The above example will output:

Setting [a] to 100
OK!
Getting [a]
Returning: 100
Setting [a] to 101
OK!
Getting [z]
Nothing!
Setting [z] to 1
Not OK!
object(Setter)#1 (2) {
    ["n"]=>
    int(1)
    ["x:private"]=>
    array(3) {
        ["a"]=>
        int(101)
        ["b"]=>
        int(2)
        ["c"]=>
        int(3)
    }
}

Method overloading

mixed __call ( string name, array arguments )

Class methods can be overloaded to run custom code defined in your class by defining this specially named method. The $name parameter used is the name as the function name that was requested to be used. The arguments that were passed in the function will be defined as an array in the $arguments parameter. The value returned from the __call() method will be returned to the caller of the method.

Example 19-19. overloading with __call example

<?php
class Caller
{
    
private $x = array(1, 2, 3);

    function
__call($m, $a)
    {
        print
"Method $m called:\n";
        
var_dump($a);
        return
$this->x;
    }
}

$foo = new Caller();
$a = $foo->test(1, "2", 3.4, true);
var_dump($a);
?>

The above example will output:

Method test called:
array(4) {
    [0]=>
    int(1)
    [1]=>
    string(1) "2"
    [2]=>
    float(3.4)
    [3]=>
    bool(true)
}
array(3) {
    [0]=>
    int(1)
    [1]=>
    int(2)
    [2]=>
    int(3)
}

Php language.oop5.overloading Function syntax tag

language.oop5.overloading php code on this is provided for your study purpose, it will guide you to know how create and design a website using php. use it to practice and train your self online



Php language.oop5.overloading syntax tutorial

php tutorial guide and code design are for easy learning and programming. The code practice section provided at the top is for practising of this syntax. Use the code section up to practice your php programming online. Learning php is very easy, all you need is to use the examples on this site and practice them to perfect your skills.


Function index

Variables

Expressions

Operators

Control-Structures

Cookies

Array

Date & Time

Directory function

File

Image

Operators

Form data handling

Mathematics operators

Mail

Php Mysql

Network Functions

Strings

php tutorial guides,functions, classes, code examples and tags for creating simple dynamic site to mysql database driven sites
 
 
Htaccess    html   Php    javascript   Asp   css    maths  Past Questions  Practice Tests Online