Programing

개체가 아닌 멤버 함수 호출

lottogame 2020. 11. 12. 07:43
반응형

개체가 아닌 멤버 함수 호출


이 질문에 이미 답변이 있습니다.

그래서 더 많은 OOP를 구현하기 위해 코드를 리팩토링하고 있습니다. 페이지 속성을 보유하는 클래스를 설정했습니다.

class PageAtrributes 
{
  private $db_connection;
  private $page_title;

    public function __construct($db_connection) 
    {
        $this->db_connection = $db_connection;
        $this->page_title = '';
    }

    public function get_page_title()
    {
        return $this->page_title;
    }

    public function set_page_title($page_title)
    {
        $this->page_title = $page_title;
    }
}

나중에 set_page_title () 함수를 이렇게 호출합니다.

function page_properties($objPortal) {    
    $objPage->set_page_title($myrow['title']);
}

오류 메시지가 표시되면 :

비 객체에서 멤버 함수 set_page_title () 호출

그래서 나는 무엇을 놓치고 있습니까?


그것은 $objPage객체의 인스턴스가 아님을 의미 합니다. 변수를 초기화하는 데 사용한 코드를 볼 수 있습니까?

특정 객체 유형을 예상했듯이 PHP 유형 힌트 기능 문서 를 사용하여 로직 위반시 오류를 얻을 수도 있습니다.

function page_properties(PageAtrributes $objPortal) {    
    ...
    $objPage->set_page_title($myrow['title']);
}

이 함수는 PageAtrributes첫 번째 매개 변수 만 허용 합니다.


이 오류를 생성하는 쉬운 방법이 있습니다.

    $joe = null;
    $joe->anything();

Will render the error:

Fatal error: Call to a member function anything() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/casMail/dao/server.php on line 23

It would be a lot better if PHP would just say,

Fatal error: Call from Joe is not defined because (a) joe is null or (b) joe does not define anything() in on line <##>.

Usually you have build your class so that $joe is not defined in the constructor or


Either $objPage is not an instance variable OR your are overwriting $objPage with something that is not an instance of class PageAttributes.


It could also mean that when you initialized your object, you may have re-used the object name in another part of your code. Therefore changing it's aspect from an object to a standard variable.

IE

$game = new game;

$game->doGameStuff($gameReturn);

foreach($gameArray as $game)
{
   $game['STUFF']; // No longer an object and is now a standard variable pointer for $game.
}



$game->doGameStuff($gameReturn);  // Wont work because $game is declared as a standard variable.  You need to be careful when using common variable names and were they are declared in your code.

I recommend the accepted answer above. If you are in a pinch, however, you could declare the object as a global within the page_properties function.

$objPage = new PageAtrributes;

function page_properties() {
    global $objPage;
    $objPage->set_page_title($myrow['title']);
}

function page_properties($objPortal) {    
    $objPage->set_page_title($myrow['title']);
}

looks like different names of variables $objPortal vs $objPage


you can use 'use' in function like bellow example

function page_properties($objPortal) use($objPage){    
    $objPage->set_page_title($myrow['title']);
}

I realized that I wasn't passing $objPage into page_properties(). It works fine now.

참고URL : https://stackoverflow.com/questions/54566/call-to-a-member-function-on-a-non-object

반응형