Правильная загрузка классов PHP - PullRequest
0 голосов
/ 03 мая 2018

Я изучаю ООП на PHP и столкнулся с проблемой правильной загрузки классов. У меня есть форма регистрации в файле 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 голосов
/ 03 мая 2018

Если у вас есть файл класса, внутри должен быть только класс, а не код. Так что в вашем случае у вас есть в основном файлы:

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();
0 голосов
/ 03 мая 2018

Вы не должны инициализировать класс в том же файле вашего класса. Переместите ваши классы в их собственные файлы.

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