웹 스터디

클래스 맴버 만들기 (static)

토픽 웹 스터디 > PHP

수업소개

지금까지는 인스턴스의 맴버를 만드는 방법을 살펴봤습니다. 맴버란 인스턴스에 소속된 속성(변수)과 메소드를 의미합니다. 이번 시간에는 클래스의 맴버를 만드는 방법을 알아봅니다. static이라는 키워드가 핵심 문법입니다.

수업

코드 

<?php
class Person{
  private static $count = 0;
  private $name;
  function __construct($name){
    $this->name = $name;
    self::$count = self::$count + 1;
  }
  function enter(){
    echo "<h1>Enter ".$this->name." ".self::$count."th</h1>";
  }
  static function getCount(){
    return self::$count;
  }
}
$p1 = new Person('egoing');
$p1->enter();
$p2 = new Person('leezche');
$p2->enter();
$p3 = new Person('duru');
$p3->enter();
$p4 = new Person('taiho');
$p4->enter();
echo Person::getCount();

 

댓글

댓글 본문