-
728x90
PHP 에서 static 은 java 와 코드 작성법이 다르다
static 변수를 사용하기 위해서는 self::$변수명 으로 사용해야한다
또는 클래스명::$변수명 으로 사용가능하다
예제를 통해 알아보자
<?php class Person { private $name; private static $count = 0; function __construct($name) { $this->name = $name; self::$count += 1; } function enter() { echo "<h1>Enter ".$this->name.", Count : ".self::$count."</h1>"; } static function getCount() { return Person::$count; } } $p = new Person("choi"); $p->enter(); $p2 = new Person("kim"); $p2->enter(); $p3 = new Person("jang"); $p3->enter(); echo Person::getCount(); ?>
결과
728x90728x90'PHP' 카테고리의 다른 글
PHP parameter 및 return 지정법 (0) 2020.12.11 require_once, namespace : import 시키는 방법 (0) 2020.12.11 SPL : Standard PHP Library (SPL) (0) 2020.12.10 PHP Redirect 하는 법 (0) 2020.12.10