「PHP」コンストラクタを使用するサンプル

書式
public function __construct(引数名)
使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<?php
class User
{
public $userName;
//コンストラクタ
public function __construct($name)
{
$this->userName = $name;
}
}
//new演算子
$gg = new User("AA001");
echo "result" . "<br>";
print $gg->userName;
<?php class User { public $userName; //コンストラクタ public function __construct($name) { $this->userName = $name; } } //new演算子 $gg = new User("AA001"); echo "result" . "<br>"; print $gg->userName;
<?php
class User
{
    public $userName;
      //コンストラクタ
    public function __construct($name)
    {
        $this->userName = $name;
    }
}
//new演算子
$gg = new User("AA001");
 echo "result" . "<br>";
print $gg->userName;

実行結果
result
AA001

PHP

Posted by arkgame