コードリーディングをしていて、下記のように文字列を返すだけのメソッド getValue
を用意しているコードがありました。
php
1<?php 2 3class Test 4{ 5 protected $a; 6 7 public function __construct() 8 { 9 $this->a[$this->getValue()] = 'テスト'; 10 } 11 12 public function getValue() 13 { 14 return 'test'; 15 } 16} 17 18$a = new Test; 19 20var_dump($a);
object(Test)#1 (1) { ["a":protected]=> array(1) { ["test"]=> string(9) "テスト" } }
プロパティでも問題はないと思いますが、メソッドにするメリットはなんでしょうか?
教えてくださいm(__)m
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2019/03/17 10:53