2012-06-28 4 views
0

비 객체에서 combinestring() 멤버 함수를 호출 할 때 문제가 발생했습니다.비 객체에서 combinestring()을 호출합니다.

**Index.php** 
inlcude("string.php"); 
calldata('usa'); 


**string.php** 
$a=new a(); 
funciton calldata($val){ 
$st1="select a from table 1 where country=".$a->combinestring($val); 
return $st1; 
} 

**Class A** 
function combinestring($abc){ 
    Return "'".$abc."'"; 
} 

unknow $ a-> combinestring ($ val);

이 문제를 해결하는 방법.

안부

당신이 변수에 멤버 함수를 호출하기 때문에

답변

0

당신은 오류

전화가 아닌 객체

에 멤버 함수 combinestring()에 있어요 그것은 대상이 아닙니다. 즉, $a은 개체가 아닙니다.

변수에 로컬 범위가 있기 때문에 string.php에서 $a 함수 정의 안에 사용할 수 없습니다. 그런 객체 인스턴스에 액세스 할 수 없습니다. 그러나 전역 변수를 사용하여이 작업을 수행 할 수 있습니다.

귀하의 string.php 파일은 다음과 같아야합니다

$a=new a(); 
funciton calldata($val){ 
    global $a; 
    $st1="select a from table 1 where country=".$a->combinestring($val); 
    return $st1; 
} 

헤드 링크에 변수 범위에 대한 자세한 내용은 : http://php.net/manual/en/language.variables.scope.php

0

사용 PDO.

funciton calldata($val){ 
    $st1="select a from table 1 where country = ?"; 
    $pdo = new PDO('host', 'user', 'pass'); 
    $result = $pdo->prepare($st1)->execute($val); 
    return $result; 
} 

당신이하고있는 일에서 많은 차이가 있지만 a 클래스는 쿼리 입력을 탈출하지 않고 그 나쁜.

관련 문제