programing

PHP 네임스페이스 및 "사용"

coolbiz 2022. 9. 28. 23:21
반응형

PHP 네임스페이스 및 "사용"

네임스페이스와 네임스페이스에 약간의 문제가 있습니다.use진술들.

3개의 파일이 있습니다.ShapeInterface.php,Shape.php그리고.Circle.php.

상대 경로를 사용하여 이 작업을 수행하려고 하기 때문에 모든 수업에 이 작업을 수행했습니다.

namespace Shape; 

서클 클래스에는 다음과 같은 것이 있습니다.

namespace Shape;
//use Shape;
//use ShapeInterface;

include 'Shape.php';
include 'ShapeInterface.php';    

class Circle extends Shape implements ShapeInterface{ ....

를 사용하는 경우include문장은 오류가 없습니다.제가 한번 해보면use받는 문장은 다음과 같습니다.

치명적인 오류: 클래스 'Shape\'/Users/shawn/Documents/work/sites/workspace/shape/Circle에서 쉐이프를 찾을 수 없습니다.8행의 php

그 문제에 대해 누가 좀 지도해 주실 수 있나요?

연산자는 클래스, 인터페이스 또는 기타 네임스페이스 이름에 별칭을 지정합니다.대부분의.use문은 축소할 네임스페이스 또는 클래스를 나타냅니다.

use My\Full\Namespace;

는 다음과 같습니다.

use My\Full\Namespace as Namespace;
// Namespace\Foo is now shorthand for My\Full\Namespace\Foo

이 경우,useoperator는 클래스 또는 인터페이스 이름과 함께 사용되며 다음과 같은 용도로 사용됩니다.

// after this, "new DifferentName();" would instantiate a My\Full\Classname
use My\Full\Classname as DifferentName;

// global class - making "new ArrayObject()" and "new \ArrayObject()" equivalent
use ArrayObject;

use오퍼레이터는 자동 로딩과 혼동해서는 안 됩니다.클래스는 자동 로드됩니다(필요 없음).include자동 로더를 등록함으로써(예:spl_autoload_registerPSR-4 를 읽고, 적절한 오토 로더의 실장을 확인해 주세요.

코드를 네임스페이스로 정렬해야 할 경우 키워드만 사용합니다.namespace:

파일1. 파일

namespace foo\bar;

file2에 있습니다.php

$obj = new \foo\bar\myObj();

를 사용할 수도 있습니다.use. file2에 넣을 경우

use foo\bar as mypath;

사용할 필요가 있다mypath대신bar파일 내의 임의의 장소:

$obj  = new mypath\myObj();

사용.use foo\bar;와 동등하다use foo\bar as bar;.

언급URL : https://stackoverflow.com/questions/10542012/php-namespaces-and-use

반응형