라이브러리

[PHP] ReflectionClass::getFileName - 클래스가 정의된 파일의 파일 이름을 가져옵니다.




ReflectionClass::getFileName


PHP의 ReflectionClass는 PHP의 클래스, 인터페이스, 함수, 상수 등에 대한 정보를 제공하는 클래스입니다. ReflectionClass::getFileName은 클래스의 파일 이름을 반환하는 메소드입니다.

# 사용법


ReflectionClass::getFileName은 ReflectionClass의 인스턴스를 생성하여 해당 인스턴스의 getFileName 메소드를 호출하여 사용할 수 있습니다.

# 예제


#hostingforum.kr
php

// myclass.php

class MyClass {

    public function sayHello() {

        echo "Hello, World!";

    }

}



// main.php

require_once 'myclass.php';



$reflectionClass = new ReflectionClass('MyClass');

echo $reflectionClass->getFileName(); // myclass.php



# 설명


위 예제에서, main.php 파일에서 myclass.php 파일의 MyClass 클래스를 사용하고자 합니다. MyClass 클래스의 파일 이름을 가져오기 위해 ReflectionClass::getFileName 메소드를 사용합니다.

# 예제 2 (인스턴스 생성)


#hostingforum.kr
php

// myclass.php

class MyClass {

    public function sayHello() {

        echo "Hello, World!";

    }

}



// main.php

require_once 'myclass.php';



$obj = new MyClass();

$reflectionClass = new ReflectionClass(get_class($obj));

echo $reflectionClass->getFileName(); // myclass.php



# 설명


위 예제에서, main.php 파일에서 myclass.php 파일의 MyClass 클래스를 사용하고자 합니다. MyClass 클래스의 인스턴스를 생성하고, 인스턴스의 클래스 정보를 가져오기 위해 ReflectionClass::getFileName 메소드를 사용합니다.

# 예제 3 (인터페이스)


#hostingforum.kr
php

// myinterface.php

interface MyInterface {

    public function sayHello();

}



// myclass.php

class MyClass implements MyInterface {

    public function sayHello() {

        echo "Hello, World!";

    }

}



// main.php

require_once 'myinterface.php';

require_once 'myclass.php';



$reflectionInterface = new ReflectionClass('MyInterface');

echo $reflectionInterface->getFileName(); // myinterface.php



$reflectionClass = new ReflectionClass('MyClass');

echo $reflectionClass->getFileName(); // myclass.php



# 설명


위 예제에서, main.php 파일에서 myinterface.php 파일의 MyInterface 인터페이스를 사용하고자 합니다. MyInterface 인터페이스의 파일 이름을 가져오기 위해 ReflectionClass::getFileName 메소드를 사용합니다. 또한, MyInterface 인터페이스를 구현하는 MyClass 클래스의 파일 이름을 가져오기 위해 ReflectionClass::getFileName 메소드를 사용합니다.

결론


ReflectionClass::getFileName은 클래스의 파일 이름을 반환하는 메소드입니다. PHP의 클래스, 인터페이스, 함수, 상수 등에 대한 정보를 제공하는 ReflectionClass를 사용하여, 클래스의 파일 이름을 가져올 수 있습니다.
  • profile_image
    나우호스팅 @pcs8404 

    호스팅포럼 화이팅!

    댓글목록

    등록된 댓글이 없습니다.

  • 전체 10,077건 / 291 페이지

검색

게시물 검색