dou12754 2013-03-02 03:46
浏览 49
已采纳

从外部函数调用php方法会产生错误

function KeepSamePage($text)
 {
$sb_w = $oPdf->GetStringWidth($text);

$num_lines = explode("
",$text);

$total = 0;
foreach($num_lines as $line)
 {

  $y = $oPdf->GetY();
  $page_height = 11 * 25.4;

  $this_width = $oPdf->GetStringWidth(strip_tags($line));
  $extra_line = floor($this_width / $w);
  $is_line = $this_width / ($w - 1);
  $is_line = $this_width == 0 ? 1 + $extra_line : ceil($is_line) + $extra_line;
  $total = $total + $is_line;
 }

  $sb_height = $total * 5;
  if(($page_height - $y) < $sb_height){ $oPdf->AddPage(); }
}

KeepSamePage($signature_block);

I'm using FPDF and I'm creating a function to keep the signature page of a letter all on the same page. This checks to see if it would go to the next page and if soo, then it does an AddPage();

The issue I'm having is that when I don't have it in a function, it works perfectly, but when I put it within a function, I get errors when calling the methods in the class represented by $oPdf.

So, my question generally is this: Is it possible to have a regular function in PHP call a class method as I have below? If it is possible, what am I doing wrong?

ERROR GENERATED IS:

Fatal error: Call to a member function GetStringWidth() on a non-object in /home/jarodmo/public_html/cms/attorney_signature_block.php on line 18

Oh, and an explanation of my function just in case you're interested or someone else finds it.

Text has for new lines in it so the PDF will put the text of the signature block on the next line. Each new array element should be a new line, so I would need to multiply the number of lines by my line height, 5 in this case. (See $total * 5).

I check to see where we are on the page, find the difference between the page height and the Y position, then check that against the height of the signature block. If the signature block is bigger, then it wouldn't fit and I know we need a manual page break.

Also, because I do the explode with the to see the lines, I also have to check to make sure that none of the lines is still wider than the page otherwise it would word wrap and really be 2 lines (or more) where I was only counting it as 1 because it was just one array element. I know a signature block shouldn't have text wide enough to be on 2 lines, but I wrote this to be applicable for more than just signature blocks. I wanted to be able to call this function anywhere I wanted to make sure certain text stayed on the same page. Call the function, check the text I'm about to write to the PDF and move on knowing that the desired text would all be on the same page.

Thanks for all of the help and comments. SO is the best.

  • 写回答

3条回答 默认 最新

  • dongyi2993 2013-03-02 03:57
    关注

    Try the following:

    function KeepSamePage($text) {
        global $oPdf;
    
        …
    }
    

    The problem is, that the object is defined outside your function and you will have to allow your function to access it.

    // Edit: If you want to avoid global for whatever reason, you will have to pass your object to the function like this:

    function KeepSamePage($text, $oPdf) {
        …
        // IMPORTANT! $oPdf has changed in this function, so you will have to give it back
        return $oPdf;
    }
    

    You can call your function like this:

    $oPdf = KeepSamePage($signature_block, $oPdf);
    

    The advantage is, that you see in the main thread, that your function might has changed the object.

    // Edit 2: I think, I was wrong on the edit1 in your case. As you pass the complete object to the function every change does apply to the object, so the changes will still be existant without giving back the result. If this was a variable that was defined in the main thread, you would have to give back the new value:

    $a = 1;
    
    function result1($a) {
        ++$a;
    }
    
    function result2($a) {
        return ++$a;
    }
    
    echo $a."
    "; // 1
    result1($a);
    echo $a."
    "; // 1
    $a = result2($a);
    echo $a."
    "; // 2
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 个人网站被恶意大量访问,怎么办
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制