正确 class 加载 PHP

Correct class loading PHP

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

这是我的 parent class,我在其中初始化 child object。

<?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();

这是一个childclass

<?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()

如你所见,child class 的 object 被初始化了两次,否则,我得到这个错误 Call to a member function addLine () on a non-object,就像我没有创建一个object。在我看来,这是错误的方法。我只想初始化 object 一次。 告诉我如何正确连接 classes? 有没有更好的方法来加载和初始化 classes?

您不应在与 class 相同的文件中初始化 class。将您的 classes 移动到他们自己的文件中。

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();

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

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();