开发者

PHP FPDF - "call to a member function Stuff() on a non-object"

So basically what I have been doing is convert one xml file to a well-formed pdf with wamp. The codes below are simple illustration of my idea开发者_开发问答.I learnt from the process of parsing xml to html and I suppose that the thing is all about telling xml parser the ways you would like to display on the screen.So then I did it like below:

<?php 
 require('fpdf.php');

 class PDF extends FPDF
 {
   //...I skipped couple of functions like Footer()

   //Display stuff from string in pdf manner
   function Stuff($data)
   {
       $this->SetFont('Arial','',12);
       $this->Cell(0,4,"$data",0,1,'L',true);
       $this->Ln(1);
   }
 }

 //Parse xml start tags
 function start($parser,$element_name,$element_data)
 {
      ......
 }    

 //Parse xml end tags
 function stop($parser,$element_name)
 {
      ......
 }

 //Parse xml contents between start tags and end tags
 function data($parser,$data)
 {
      $pdf->Stuff($data);
 }

 //create pdf page
 $pdf = new PDF();
 $pdf->AliasNbPages();
 $pdf->AddPage();
 $pdf->SetFont('Times','',12);

 //Parse xml file to pdf
 $parser=xml_parser_create();
 xml_set_element_handler($parser,"start","stop");
 xml_set_character_data_handler($parser,"char");
 $fp=fopen("test.xml","r");

 while($data=fread($fp,4096))
 {
     xml_parse($parser,$data,feof($fp)) or 
     die (sprintf("XML Error:$s at line $d",
     xml_error_string(xml_get_error_code($parser)),
     xml_get_current_line_number($parser)));
 }

 xml_parser_free($parser);

?>

Unfortunately I received this warning as I described in the title.However,I tested my xml parser functions with my start,stop and data functions individually,they all worked fine.Then I tried to convert xml to html,everything is still good.It is primarily converting to pdf that I encounter this situation when working on my idea. Could anyone give me some hints about this problem?


In your data function, you reference $pdf->Stuff() but there is no variable $pdf so that is why you see the error. Because $pdf is not an object because it doesn't exist. Did you mean $parser->Stuff($data)? or if that function is inside of your class, then maybe it should be $this->Stuff($data)

You can have the handlers in your class, you just need to specify them a bit differently. You can do

xml_set_object ( $parser, $this );
xml_set_element_handler ( $parser, 'start', 'stop' );
xml_set_character_data_handler ( $parser, 'char' );
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜