오늘은 공부 중/PHP

[php] $this와 self:: 그리고 static과 dynamic

이도토리 2021. 7. 12. 21:23
<?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."</h1>";
}

static function getCount(){
  return self::$count;
}
}

$p1 = new Person('d');
$p1->enter();
$p2 = new Person('m');
$p2->enter();
$p3 = new Person('j');
$p3->enter();
$p4 = new Person('s');
$p4->enter();
echo Person::getCount();
?>

static 정적인

dynamic 동적인

 

instance가 생성될 때는 자동으로 동적으로 만들어짐

하지만 static이 붙으면 class에 소속되는 것.

 

static : class 소속 (class property. static property) - 모든 instance가 공유 가능. 클래스가 직접 호출 가능.

dynamic : instance 소속 멤버 제작.

 

 

$this -> 인스턴스에 대한 자기자신

self::  클래스에 대한 자기자신