What is static and how do I use it? [duplicate]

0

I have a lot of doubts about how to use static in an attribute, if anyone can get me out of this doubt, I would appreciate it a lot, thanks.

    
asked by anonymous 03.08.2015 / 23:54

1 answer

0

Keyword 'static'

Declaring members or methods of a class as static makes them accessible without instantiating the class. A member declared as static can not be accessed with an instantiated object of the class (although static methods can).

Exemplo #1 Exemplo de membro estático

<?php
class Foo
{
    public static $meu_estatico = 'foo';

    public function valorEstatico() {
        return self::$meu_estatico;
    }
}


class Bar extends Foo
{
    public function fooEstatico() {
        return parent::$meu_estatico;
    }
}


print Foo::$meu_estatico . "\n";

$foo = new Foo();
print $foo->valorEstatico() . "\n";
print $foo->$meu_estatico . "\n";      // "Propriedade" Indefinida $meu_estatico

print $foo::$meu_estatico . "\n";
$classname = 'Foo';
print $classname::$meu_estatico . "\n"; // No PHP 5.3.0

print Bar::$meu_estatico . "\n";
$bar = new Bar();
print $bar->fooEstatico() . "\n";
?>

Reference: link

    
04.08.2015 / 00:05