OOP 개념을 처음 접했으니 제 생각에는 틀린 방식 일 수 있습니다.PHP의 클래스 초기화에 선택적 구성 변수 전달
저는 꽤 자주 사용하는 일부 기능을위한 클래스를 개발하고 있으며 초기화시 새 프로젝트에서 구성 가능하도록하고 싶습니다. 여기서주의해야 할 점은 특정 기본 변수를 설정하고 기본 값이 괜찮은 경우 해당 변수를 구성 해제 된 상태로 유지할 수 있다는 것입니다. 개념을 좀 더 분명하게하려고하는 약간의 코드가 있습니다.
class someClass{
// Setting parameter defaults
private $param_a = 60;
private $param_b = 100;
/*
* The construct function. What I'd like to do here is make the param_a and param_b optional,
* i.e if it doesn't get set on initialization it takes the defaults from the class.
*/
function __construct($param_a, $param_b, $foo){
// do something ...
}
}
$foo = "some value";
// init example using defaults
$someclass = new someClass($foo); // $param_a and $param_b should be 60 and 100 respectively
// init example using custom options
$someclass = new someClass(40, 110, $foo);
클래스 구성 설정 방법에 관해서는 올바른 방향으로 가고 있습니까? 그렇다면 어떻게 param_a와 param_b를 선택적으로 만들 수 있습니까?
덕분에 많이 –