"Sputnik" help  
Sputnik Help
Language Reference - Classes - Class Magic Functions - Call

Class Magic Functions - Call

This allows a class to accept calls to functions that simply dont exist inside the class kinda like a catch all for example:

class Test
{
    Function __Call($name, $arg)
    {
        switch ($name)
        {
            case "cat":
                say "Cat says $arg";
                break;
            case "dog":
                say "Dog says $arg";
                break;
            default:
                say "User tried to call a function called $name";
                break;
        }
    }
}

my $a = new Test();
$a->Cat("Meow");
$a->Dog("Woof");
$a->Sheep("Baaa");

// PRINTS
// Cat says Meow
// Dog says Woof
// User tried to call a function called sheep

Notice "sheep" is lower case in the error message? Sputnik is case insensitive so it will make sure the input is always converted to lower case.

It is possible to do infinite arguments and anything else you want for example:

class Test
{
    Function __Call($name, '.' $values)
    {
        my $count = (count)$values;
        my $sum = 0;
        foreach(my $var in $values)
            $sum += $var;
        switch ($name)
        {
            case "cat":
                say "Cat adds up $sum";
                break;
            case "dog":
                say "Dog adds up $sum";
                break;
            default:
                say "User tried to call a function called $name";
                break;
        }
    }
}

my $a = new Test();
$a->Cat(10, 50, 100);
$a->Dog(2, 10, 5);
$a->Sheep();

// PRINTS
// Cat adds up 160
// Dog adds up 17
// User tried to call a function called sheep

This is similar to User Variable Functions.

A full list of all possible magic functions can be found here.


Contact
Cheryl (uberfox@hotmail.com)
Homepage
http://ubersoft.org