본문 바로가기
IT/php

2021-01-26-오버로딩

by 봉즙 2023. 2. 27.

layout : post
title : "오버로딩"

category : PHP

PHP에서는 다른 언어와는 달리 프로퍼티나 메소드를 동적으로 '생성한다'는 의미로 오버로딩을 사용

동적으로 생성된 멤버는 해당 클래스의 매직 메소드(magic method)를 통해 다양한 형태로 처리할 수 있다. 오버로딩되는 메소드는 반드시 public으로 정의해야 한다.

프로퍼티 오버로딩(property overloading)

PHP에서는 접근 불가 프로퍼티(inaccessible property)를 오버로딩하기 위해 다음과 같은 매직 메소드를 구현해야 한다.

1. public void __set(string $name, mixed $value)

2. public mixed __get(string $name)

3. public bool __isset(string $name)

4. public bool __unset(string $name)
  1. __set() 메소드는 접근 불가 프로퍼티의 값을 설정할 때 호출

  2. __get() 메소드는 접근 불가 프로퍼티의 값을 읽을 때 호출

  3. __isset() 메소드는 접근 불가 프로퍼티에 대해 isset() 함수나 empty() 함수가 호출될 때 호출

  4. __unset() 메소드는 접근 불가 프로퍼티에 대해 unset() 함수가 호출될 때 호출

메소드 오버로딩(method overloading)

PHP에서는 접근불가 메소드(inaccessible method)를 오버로딩하기 위해 다음과 같은 매직 메소드를 구현

1. public mixed __call(string $name, array $arguments)

2. public static mixed __callStatic(string $name, array $arguments)
  1. __call() 메소드는 클래스 영역에서 접근 불가 메소드를 호출할 때 호출됩니다.

  2. __callStatic() 메소드는 정적(static) 영역에서 접근 불가 메소드를 호출할 때 호출됩니다.

'IT > php' 카테고리의 다른 글

2021-01-26-조건문  (0) 2023.02.27
2021-01-26-인터페이스  (0) 2023.02.27
2021-01-26-상수  (0) 2023.02.27
2021-01-26-상속  (0) 2023.02.27
2021-01-26-변수  (0) 2023.02.27

댓글