"Sputnik" help  
Sputnik Help
Language Reference - Classes - Static Classes

Static Classes

Static class features make it so you can access variables and functions outside of the class.

In Sputnik all class functions etc are totally independant and do not rely on the class itself to function however the class acts kind of a collection to keep everything in one place this is why you can reference class functions and use them outside of a class.

For example so its possible to grab them functions and make use of them outside of their class see this example:

class foo
{
    Function bar()
    {
        say "Foo Bar!!!";
    }
}

class Cat
{
    my $name;
    my $lives;
    Function __Construct($n, $l)
    {
        $name = $n;
        $lives = $l;
    }
    Function Meow()
    {
        say "$name says Meow!";
        foo:bar();
    }
}


my $felix = new Cat("Felix");
$felix->Meow();

// PRINTS
// Felix says Meow!
// Foo Bar!!!
Notice the function "bar" in the class "foo" is called from the class "Cat" directly? Technically the way this works is it creates a reference to the function then calls it this demonstrates it better:
class foo
{
    Function bar()
    {
        say "Foo Bar!!!";
    }
}

class Cat
{
    my $name;
    my $lives;
    Function __Construct($n, $l)
    {
        $name = $n;
        $lives = $l;
    }
    Function Meow()
    {
        say "$name says Meow!";
        $ref = &foo:bar;
        $ref();
    }
}


my $felix = new Cat("Felix");
$felix->Meow();

// PRINTS
// Felix says Meow!
// Foo Bar!!!

Class functions can be used anywhere like so:

class Cat
{
    Function Meow()
    {
        say "Meow!";
    }
}

Cat:Meow();

// PRINTS
// Meow!

This can be used to make classes that can be used as a library for functions it works similar to static functions in other languages without needing to use a static keyword since all functions are exposed.

We can also make use of static variables inside classes for example here we create a static variable for the class then run a function from the class in static mode and make use of the classes static variable.

class Cat
{
    Function Meow()
    {
        for (my $i = 0; $i < Cat:$meows; $i++)
        {
            say "Meow!";
        }
    }
}

Cat:$meows = 3;
Cat:Meow();

// PRINTS
// Meow!
// Meow!
// Meow!

Notice the variable "meows" did not need to be defined in the class itself? This is because it will simply be created if it does not exist.

If we wish to have variables pre-existing (ie create them when the class is defined) we can do that by using he ConstructStatic Magic Function like so:

class Cat
{
    Function __ConstructStatic()
    {
        Cat:$meows = 3;
    }
    Function Meow()
    {
        say "Meow! x" . Cat:$meows;
    }
}

Cat:Meow();
Cat:$meows = 10;
Cat:Meow();

// PRINTS
// Meow! x3
// Meow! x10

Notice the class itself ran the static constructor when it was defined which set the "meows" variable to 3 by default.

Note: Sputnik does not have terms like base/self/parent etc instead you must name the class directly even if you are already inside the class.

A more simple way to create a static variable in a class is to simply define it directly using the static variable system:

class Cat
{
    static $meows = 3;
    Function Meow()
    {
        say "Meow! x" . Cat:$meows;
    }
}

Cat:Meow();
Cat:$meows = 10;
Cat:Meow();

// PRINTS
// Meow! x3
// Meow! x10

This can be used help with the creation of libraries that store some useful variables inside a class in a static way things outside the class can access.


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