简体   繁体   中英

Can spl_autoload_register() be used to add external functions in FPDF?

I would like to put my item creation function code into their own file and include them in my main PDF formatting class...

Is this possible in FPDF? I would like to keep the functions below out of my main FPDF creator class .... I will have 5 or 6 categories with possibly 20 items and adding all that code would make the PDF creator class very large...

(why the heck can't we just use "include or require") Couldn't an "autoload" feature be added directly to PHP so we can just use "include or require" to add stuff back in? :)

If I create a class with all the item functions inside, can this be loaded into the pdf creator class?

class ladiesClass {

function Item1() {
    if (isset($_POST["item1Qty"][0]) && !empty($_POST["item1Qty"][0]) || 
    isset($_POST["item1Qty"][1]) && !empty($_POST["item1Qty"][1]) ||
    isset($_POST["item1Qty"][2]) && !empty($_POST["item1Qty"][2]) ||
    isset($_POST["item1Qty"][3]) && !empty($_POST["item1Qty"][3]) ||
    isset($_POST["item1Qty"][4]) && !empty($_POST["item1Qty"][4]) ||
    isset($_POST["item1Qty"][5]) && !empty($_POST["item1Qty"][5])) {
    $item1Qty = $_POST['item1Qty'];
    $item     = $_POST['item1'];
    $desc     = $_POST['desc1'];
    $color    = $_POST['color1'];
    $this->SetFont('Arial', '', 10);
    $this->SetFillColor(242);
    $this->SetLineWidth(1); 
    $this->SetX(33);
    $this->Cell(97, 20, $item, 'LRB', 0, 'L');
    $this->Cell(300, 20, $desc, 'LRB', 0, 'L');
    $this->Cell(95, 20, $color, 'LRB', 0, 'L');
    $this->Cell(28, 20, $item1Qty[0], 'LRB', 0, 'C');
    $this->Cell(28, 20, $item1Qty[1], 'LRB', 0, 'C');
    $this->Cell(28, 20, $item1Qty[2], 'LRB', 0, 'C');
    $this->Cell(28, 20, $item1Qty[3], 'LRB', 0, 'C');
    $this->Cell(28, 20, $item1Qty[4], 'LRB', 0, 'C');
    $this->Cell(28, 20, $item1Qty[5], 'LRB', 0, 'C');
    $this->Cell(28, 20, '', 'LRB', 0, 'C');
    $this->Cell(38, 20, array_sum($item1Qty), 'LRB', 1, 'C');        
}
}
//   ITEM 2
function Item2() {
    if (isset($_POST["item2Qty"][0]) && !empty($_POST["item2Qty"][0]) || 
    isset($_POST["item2Qty"][1]) && !empty($_POST["item2Qty"][1]) ||
    isset($_POST["item2Qty"][2]) && !empty($_POST["item2Qty"][2]) ||
    isset($_POST["item2Qty"][3]) && !empty($_POST["item2Qty"][3]) ||
    isset($_POST["item2Qty"][4]) && !empty($_POST["item2Qty"][4]) ||
    isset($_POST["item2Qty"][5]) && !empty($_POST["item2Qty"][5])) {
    $item2Qty = $_POST['item2Qty'];
    $item     = $_POST['item2'];
    $desc     = $_POST['desc2'];
    $color    = $_POST['color2'];               
    $this->SetFont('Arial', '');
    $this->SetFillColor(242);
    $this->SetLineWidth(1); 
    $this->SetX(33);
    $this->Cell(97, 20, $item, 'LRB', 0, 'L');
    $this->Cell(300, 20, $desc, 'LRB', 0, 'L');
    $this->Cell(95, 20, $color, 'LRB', 0, 'L');     
    $this->Cell(28, 20, $item2Qty[0], 'LRB', 0, 'C');
    $this->Cell(28, 20, $item2Qty[1], 'LRB', 0, 'C');
    $this->Cell(28, 20, $item2Qty[2], 'LRB', 0, 'C');
    $this->Cell(28, 20, $item2Qty[3], 'LRB', 0, 'C');
    $this->Cell(28, 20, $item2Qty[4], 'LRB', 0, 'C');
    $this->Cell(28, 20, $item2Qty[5], 'LRB', 0, 'C');
    $this->Cell(28, 20, '', 'LRB', 0, 'C');
    $this->Cell(38, 20, array_sum($item2Qty), 'LRB', 1, 'C');
}
}
}       //   ITEM 3, ITEM 5, etc.....

Autoloading is for classes, not function. In your case, you would have to create class for every function, fe

class Item1Creator {
    public static function create(FPDF $fpdfObject) {
       // content of your function Item1()
       // you should change $this to $fpdfObject
    }
}

Then, if you store this class into file fe items/Item1Creator.php you than can use in your cote $itm1 = Item1Creator::create() instead of $itm1 = Item1()

To make this working, autoload have to be used. Probably in your main FPDF class file you should register your autoloader by:

autload_spl_register(function($class) {
    $file = __DIR__ . '/items/' . $class . '.php';
    if (file_exists($file)) {
        include $file;
    }
});

Above solution is bad for so many reasons

  • Using objects badly. There are lot of design patterns you should learn
  • Using methods/function without parameters to load data from superglobals, instead of first read data
  • you should probably just include files with functions on the top of your FPDF class

But it should work. You have a lot to learn before you, but don't give up ;o)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM