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条)

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?