0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

[PHP]定数のプロパティだとstaticプロパティになる

Last updated at Posted at 2021-02-28
class Foo {
     public $a = "aaa";
     public const b = "bbb";
 }
 
 $foo = new Foo();
 echo $foo->a;
 echo $foo->b;

エラー

PHP Warning:  Undefined property: Foo::$b in /workspace/Main.php on line 10

phpでclassで定数(const)プロパティを使っても、staticプロパティになりnewで定義したインスタンスから呼び出すことができない

修正

class Foo {
     public $a = "aaa";
     public const b = "bbb";
 }
 
 $foo = new Foo();
 echo $foo->a;
 echo "\n";
 echo Foo::b;

結果

aaa
bbb

補足

インターフェースでは定数プロパティしか定義できず、変数プロパティは定義できない。

0
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?