正确的类加载PHP

时间:2018-05-03 20:13:03

标签: php autoload

我正在学习PHP中的OOP,我遇到了正确加载类的问题。我在HTML文件中有一个注册表单,其中的数据由PHP脚本处理。我有一个父类和几个子类,每个类都在不同的文件中。我想自动将每个子类加载到父级中。

这是我的父类,我在其中初始化子对象。

<?php

spl_autoload_register(function ($class) { /*load the child class*/
    include $class . '.php';
});

$addTxt = new AddTxt(); /*init child class object*/

class Validator {
    public $name  = 'name ';
    public $s_name  = 's_name ';
    public $email = 'email ';
    public $ticket = 'ticket ';
    function __construct(){
        $this->name = $_POST['name'];
        $this->s_name = $_POST['s_name'];
        $this->email = $_POST['email'];
        $this->ticket = $_POST['ticket'];
    }
}

$validate = new Validator();

这是一个儿童班

<?php

class AddTxt extends Validator {
    public $string = "test";
    public $file;
    public $date;
    function __construct(){
        parent::__construct();
        $this->date = date('d_m_Y');
        $this->file = 'registration_' . $this->date . ".txt";
        $this->string = $this->name . " " . $this->s_name . " " . $this->email . " " . $this->ticket . PHP_EOL;
    }
    function addLine(){
        if(file_exists($this->file)){
            $f = fopen($this->file, "a+") or die ("Error");

            if (($f) && filesize($this->file)) {
                $lines = explode("\n", fread($f, filesize($this->file)));

                foreach($lines as $line){
                    $l = explode(" ", $line);
                    $line_items[] = $l[2];
                }
                foreach ($line_items as $item) {
                    if($item === $this->email) {
                        die ("such email already exist");
                    }
                }
                fwrite($f, $this->string);
            }
            else {
                fwrite($f, $this->string);
            }

            fclose($f);
        }
        if(!file_exists($this->file)) {
            $f = fopen($this->file, "a+") or die ("Error");

            fwrite($f, $this->string);

            fclose($f);
        }
    }
}


$addTxt = new AddTxt();/*another one child class init*/
$addTxt->addLine()

如您所见,子类的对象初始化两次,否则,我得到此错误Call to a member function addLine () on a non-object,就像我没有创建对象一样。在我看来,这是错误的方式。我想只初始化一次对象。 告诉我如何正确连接类? 有没有更好的方法来加载和初始化类?

2 个答案:

答案 0 :(得分:1)

您不应该在类的同一文件中初始化类。将您的班级移动到他们自己的文件中。

Validator.php

<?php

class Validator {
    public $name  = 'name ';
    public $s_name  = 's_name ';
    public $email = 'email ';
    public $ticket = 'ticket ';

    function __construct(){
        $this->name = $_POST['name'];
        $this->s_name = $_POST['s_name'];
        $this->email = $_POST['email'];
        $this->ticket = $_POST['ticket'];
    }
}

AddTxt.php

<?php

class AddTxt extends Validator {
    public $string = "test";
    public $file;
    public $date;
    function __construct(){
        parent::__construct();
        $this->date = date('d_m_Y');
        $this->file = 'registration_' . $this->date . ".txt";
        $this->string = $this->name . " " . $this->s_name . " " . $this->email . " " . $this->ticket . PHP_EOL;
    }
    function addLine(){
        if(file_exists($this->file)){
            $f = fopen($this->file, "a+") or die ("Error");

            if (($f) && filesize($this->file)) {
                $lines = explode("\n", fread($f, filesize($this->file)));

                foreach($lines as $line){
                    $l = explode(" ", $line);
                    $line_items[] = $l[2];
                }
                foreach ($line_items as $item) {
                    if($item === $this->email) {
                        die ("such email already exist");
                    }
                }
                fwrite($f, $this->string);
            }
            else {
                fwrite($f, $this->string);
            }

            fclose($f);
        }
        if(!file_exists($this->file)) {
            $f = fopen($this->file, "a+") or die ("Error");

            fwrite($f, $this->string);

            fclose($f);
        }
    }
}

的index.php

<?php

spl_autoload_register(function ($class) { /*load the child class*/
    include $class . '.php';
});

$addTxt = new AddTxt();
$addTxt->addLine();

答案 1 :(得分:1)

如果你有一个类文件,那么应该只有里面的类而没有代码。所以在你的情况下你基本上有文件:

Validator.php

class Validator { ... }

AddTxt.php

class AddTxt extends Validator { ... }

的index.php

<?php

spl_autoload_register(function ($class) { /*load the child class*/
    include $class . '.php';
});

$addTxt = new AddTxt();
$addTxt->addLine();