메뉴 건너뛰기

프로그램언어

조회 수 288 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄

static.php

<?php
//클래스 변수
//고정된 이라는뜻
// static : 모든 인스턴스가 공유하는 변수 만들기!!
//static 변수는 class 명으로 바로 접근할 수 있다.(instance 즉 객체를 만들지 않고 접근가능하다.)

class Person{
 
private $name; //다른 객체가 접근할 수 없다. 즉 공통으로 사용할 수 없다.
 
private $count = 0;
 
//생성자: 객체가 생성될때 필요한 값을 셋팅한다.
function __construct($name){
$this->name = $name;
$this->count = $this->count +1;
}
 
function enter(){
echo "<h1>Enter ".$this->name." {$this->count} th</h1><br>";
}
 
}
 
$p1 = new Person('abcnt');
$p1 ->enter();

$p2 = new Person('하하하');
$p2 ->enter();
 
$p1 = new Person('호호호');
$p1 ->enter();

?>

 

static2.php

<?php
//클래스 변수
//고정된 이라는뜻
// static : 모든 인스턴스가 공유하는 변수 만들기!!
//static 변수는 class 명으로 바로 접근할 수 있다.(instance 즉 객체를 만들지 않고 접근가능하다.)

class Person{

//인스턴스 변수
private $name; //다른 객체가 접근할 수 없다. 즉 공통으로 사용할 수 없다.

//클래스 변수 // 모든 인스턴스가 공유한다!!
//클래스 변수에는 self::를 사용한다.
private static $count = 0;
 
//생성자: 객체가 생성될때 필요한 값을 셋팅한다.
function __construct($name){
$this->name = $name;
self::$count = self::$count +1;
}
 
function enter(){
echo "<h1>Enter ".$this->name." ".self::$count." th</h1><br>";
}
 
//클래스 이름으로 호출되려면 static을 호출해야 한다.
static function getCount(){
 
//클래스 변수에는 self를 사용한다.
return self::$count;
}
 
}

$p1 = new Person('abcnt');
$p1 ->enter();
 
$p2 = new Person('하하하');
$p2 ->enter();
 
$p1 = new Person('호호호');
$p1 ->enter();
 
//클래스 변수 호출
echo Person::getCount();
 
?>

 


 


  1. php date 날짜 관련 함수

    Date2021.03.27 Views411
    Read More
  2. 문자,숫자 랜덤 출력

    Date2021.03.26 Views407
    Read More
  3. HEREDOC <<< ( PHP에서 echo로 HTML쉽게 표시하기 )

    Date2021.03.26 Views371
    Read More
  4. php 암호화 복호화 , 간단한 암호화

    Date2023.01.12 Views366
    Read More
  5. AJAX로 해당 페이지에서 COOKIE 사용하기

    Date2021.03.26 Views359
    Read More
  6. 복권 번호 뽑기

    Date2021.03.26 Views340
    Read More
  7. mysql_free_result(); 관련 오류

    Date2021.03.25 Views340
    Read More
  8. curl을 이용하여 post, get 방식 으로 데이터 전송하기

    Date2023.01.12 Views333
    Read More
  9. TIFF, GIF 여러장 변환

    Date2021.03.26 Views324
    Read More
  10. 특정일의 주차 구하기

    Date2021.07.08 Views322
    Read More
  11. 문자열 함수 모음

    Date2021.03.26 Views318
    Read More
  12. 쿠키 확인 후 만료시 세션 파괴하는 방법

    Date2020.11.23 Views314
    Read More
  13. PHP와 HTML과 자바스크립트의 관계

    Date2021.03.26 Views311
    Read More
  14. 정규표현식

    Date2021.03.26 Views311
    Read More
  15. 확장자 추출 하기

    Date2021.03.26 Views309
    Read More
  16. PHP Notice: Use of undefined constant ... assumed ... 오류

    Date2021.03.26 Views308
    Read More
  17. 콜론 연산자

    Date2021.03.26 Views307
    Read More
  18. 비교연산자 ( === 에 관해서 )

    Date2021.03.25 Views307
    Read More
  19. PHP웹 보안 취약점 TOP5(웹해킹)

    Date2023.01.12 Views293
    Read More
  20. [PHP기초] 함수와 객체의 비교

    Date2021.03.27 Views291
    Read More
Board Pagination Prev 1 ... 8 9 10 11 12 13 14 15 16 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved