php - Quand devrais-je utiliser 39;self align#39 sur 39;$thisέ#39

Mots clés : phpclassoopscopephp

meilleur 4 Réponses php - Quand devrais-je utiliser 39;self align#39 sur 39;$thisέ#39

vote vote

91

<?php class X {     private $non_static_member = 1;     private static $static_member = 2;      function __construct() {         echo $this->non_static_member . ' '            . self::$static_member;     } }  new X(); ?> 
<?php class X {     private $non_static_member = 1;     private static $static_member = 2;      function __construct() {         echo self::$non_static_member . ' '            . $this->static_member;     } }  new X(); ?> 
<?php class X {     function foo() {         echo 'X::foo()';     }      function bar() {         $this->foo();     } }  class Y extends X {     function foo() {         echo 'Y::foo()';     } }  $x = new Y(); $x->bar(); ?> 
<?php class X {     function foo() {         echo 'X::foo()';     }      function bar() {         self::foo();     } }  class Y extends X {     function foo() {         echo 'Y::foo()';     } }  $x = new Y(); $x->bar(); ?> 
vote vote

84

class Person {     private $name;      public function __construct($name) {         $this->name = $name;     }      public function getName() {         return $this->name;     }      public function getTitle() {         return $this->getName()." the person";     }      public function sayHello() {         echo "Hello, I'm ".$this->getTitle()."<br/>";     }      public function sayGoodbye() {         echo "Goodbye from ".self::getTitle()."<br/>";     } }  class Geek extends Person {     public function __construct($name) {         parent::__construct($name);     }      public function getTitle() {         return $this->getName()." the geek";     } }  $geekObj = new Geek("Ludwig"); $geekObj->sayHello(); $geekObj->sayGoodbye(); 
vote vote

74

class Person {      public static function status()     {         self::getStatus();     }      protected static function getStatus()     {            echo "Person is alive";     }  } 
class Deceased extends Person {      protected static function getStatus()     {            echo "Person is deceased";     }  } 
vote vote

60

class Person {     public $name = 'my name';     public function sayHello() {         echo "Hello";     } } 
$bob = new Person; $adam = new Person; $bob->name = 'Bob'; echo $adam->name; // "my name" 
class Foo {     public $bar = 1; } 
class Foo {     public static $bar = 1; } 
class Foo {     public function bar() {} } 
class Foo {     public static function bar() {} } 
class Foo {     const BAR = 1; } 
$bob = new Person; echo $bob->name; 
echo Foo::bar() 
echo $foo::bar() 
$class = get_class($foo); $class::bar(); 
class Foo {     public $a = 1;     public function bar() {         return $this->a;     } } 
class Foo {     public function bar() {         return Foo::baz();     }     public function baz() {         return isset($this);     } } 
class Person {     public static $number = 0;     public $id = 0;     public function __construct() {         self::$number++;         $this->id = self::$number;     }     public $name = "";     public function getName() {         return $this->name;     }     public function getId() {         return $this->id;     } }  class Child extends Person {     public $age = 0;     public function __construct($age) {         $this->age = $age;         parent::__construct();     }     public function getName() {         return 'child: ' . parent::getName();     } } 
$bob = new Person; $bob->name = "Bob"; $adam = new Person; $adam->name = "Adam"; $billy = new Child; $billy->name = "Billy"; var_dump($bob->getId()); // 1 var_dump($adam->getId()); // 2 var_dump($billy->getId()); // 3 
var_dump($bob->getName()); // Bob var_dump($adam->getName()); // Adam var_dump($billy->getName()); // child: Billy 
class Foo {     public function isFoo() {         return $this instanceof Foo;     } } 
class Bar {     public function doSomething() {         return Foo::isFoo();     } } $b = new Bar; var_dump($b->doSomething()); // bool(false) 

Questions similaires