note 130276 added to language.oop5.static

From: Date: Wed, 30 Apr 2025 15:57:46 +0000
Subject: note 130276 added to language.oop5.static
Groups: php.notes 
Request: Send a blank email to php-notes+get-222119@lists.php.net to get a copy of this message
Although everyone will say you should not call a class static method from an instance of the class, if you do find occasion to be doing it, there are multiple syntaxes you can employ. <?php class Foo { public static method bar() { echo "OK\n"; } } $myfoo = new Foo(); ?> 1. Instance method syntax <?php $myfoo->bar(); //OK ?> This is the worst option because it looks like you are calling an instance method. 2. Static method syntax <?php $myfoo::bar(); //OK ?> This is most compact and its meaning is clear, although it might imply that $myfoo is a string containing the class's name. 3. Static method syntax with ::class (>=PHP8) <?php $myfoo::class::bar(); //OK ?> Less compact but fully semantically sound. There is no known likelihood of the first two syntaxes going away, but the third should be 100% (or 99.9r%) future proof if you are paranoid. ---- Server IP: 45.112.84.4 Probable Submitter: 80.90.5.137 (proxied: 87.75.113.64) ---- Manual Page -- https://php.net/manual/en/language.oop5.static.php Edit -- https://main.php.net/note/edit/130276 Del: integrated -- https://main.php.net/note/delete/130276/integrated Del: useless -- https://main.php.net/note/delete/130276/useless Del: bad code -- https://main.php.net/note/delete/130276/bad+code Del: spam -- https://main.php.net/note/delete/130276/spam Del: non-english -- https://main.php.net/note/delete/130276/non-english Del: in docs -- https://main.php.net/note/delete/130276/in+docs Del: other reasons-- https://main.php.net/note/delete/130276 Reject -- https://main.php.net/note/reject/130276 Search -- https://main.php.net/manage/user-notes.php

« previous php.notes (#222119) next »