• 회원가입
  • 로그인
  • 구글아이디로 로그인

[PHP-생코] PHP 9강 - 클래스 맴버 만들기 (static)

859  

[인스턴스 기호] ->

$this->변수명 : 인스턴스 용 변수.

$인스턴스명->메서드명() : 인스턴스 용 메서드 출력 코드.

 

[클래스 전용 기호] ::

static 키워드 추가해 「$변수명・메서드」 정의 후, 

self::$변수명 : 클래스 용 변수.

클래스명::메서드명() : 클래스 용 메서드 출력 코드.

 

 

<?php

class Person{

  private static $count = 0; // 클래스 소속 속성 생성

  private $name;

  function __construct($name){

    $this->name = $name;

    $this->count = $this->count + 1; // (X)

    self::$count = self::$count + 1; // (O)

  }

  function enter(){

    echo "<h1>Enter ".$this->name." {$this->count}th</h1>"; // (X)

    echo "<h1>Enter ".$this->name." ".self::$count."th</h1>"; // (O)

  }

  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 P4->getCount(); // (△)

echo Person::getCount(); // (O)

?>

 

https://opentutorials.org/module/6/15753

 

PS.

 

PHP static

https://www.php.net/manual/en/language.oop5.static.php

 

static 메서드

https://homzzang.com/b/php-1070

 

static 속성

https://homzzang.com/b/php-1073

 



분류 제목
JS-엘리 JS 5강 - Arrow Function은 무엇인가? 함수의 선언과 표현 | (JavaScript ES6)
JS-엘리 JS 4강 - 코딩의 기본 operator, if, for loop 코드리뷰 팁 | (JavaScript E…
JS-엘리 JS 3강 - 데이터타입, data types, let vs var, hoisting | (JavaScrip…
JS-엘리 JS 2강 - 콘솔에 출력, script (async・defer) 차이점 및 앞으로 자바스크립트 공부 방향
JS-엘리 JS 1강 - 자바스크립트의 역사와 현재 그리고 미래 (JavaScript, ECMAScript, JQuer…
GO-터커 GO 50강 - 마지막 강의
GO-터커 GO 49강 - Beyond OOP
GO-터커 GO 48강 - OOD의 SOLID
GO-터커 GO 47강 - Interface (인터페이스) 2
GO-터커 GO 46강 - OOP3, Interface 란?
GO-터커 GO 45강 - OOP2, Object 란?
GO-터커 GO 44강 - OOP
GO-터커 GO 43강 - Select
GO-터커 GO 42강 - channel
GO-터커 GO 41강 - DeadLock & channel
GO-터커 GO 40강 - Thread (쓰레드) 2
GO-터커 GO 39강 - Thread (쓰레드) 1
GO-터커 GO 38강 - GoLang의 Map
GO-터커 GO 37강 - Map 구현
GO-터커 GO 36강 - Map과 Hash의 관계
7/35
목록
찾아주셔서 감사합니다. Since 2012