简体   繁体   English

Class[OOP] 扩展了 PHP 中的问题

[英]Class[OOP] extends Problem in PHP

I have 3(three) class in PHP我在 PHP 中有 3(三个)class

main Class is Detection.php主要 Class 是检测。php

Detection.php检测.php

<?php
include "Helper.php";
include "Unicode.php";
class Detection {

    private $helper;
    private $unicode;

    public function __construct() {
        mb_internal_encoding('UTF-8');
        $this->helper = new Helper();
        $this->unicode = new Unicode();
    }
    public function detect($text) {
        $arrayOfChar = $this->helper->split_char($text);
        $words = $this->helper->split_word($text);
        ................
        return $xxx;
    }
    ..............
}
$i = new Detection();
?>

Helper.php助手.php

<?php
class Helper {

    public function __construct() {
    }
    public function split_word($text) {
        $array =  mb_split("\s", preg_replace( "/[^\p{L}|\p{Zs}]/u", " ", $text ));
        return $this->clean_array($array);
    }
    public function clean_array($array) {
        $array = array_filter($array);
        foreach($array as &$value) {
            $newArray[] = $value;
        } unset($value);
        return $newArray;
    }
    public function split_char($text) {
        return preg_split('/(?<!^)(?!$)/u', mb_strtolower(preg_replace( "/[^\p{L}]/u", "", $text )));
    }
    public function array_by_key($array, $key) {
        foreach($array as &$value) {
            $new_array[] = $value[$key];
        } unset($value);
        return $new_array;
    }
}
?>

Unicode.php Unicode.php

<?php
include "DatabaseConnection.php";
//include "Helper.php"; WHEN '//' double slash is removed, i got code is not working
class Unicode {

    private $connection;
    private $helper;
    public function __construct() {
        $this->connection = new DatabaseConnection();
        //$this->helper = new Helper(); WHEN '//' double slash is removed, i got code is not working
    }
.................
}
?>

In Unicode.php as you can see there is two line that i comment with double slashes '//'.在 Unicode.php 中,您可以看到有两行我用双斜杠“//”注释。 when i remove and i run the code, i got white screen in browser that mean something wrong happen.当我删除并运行代码时,我在浏览器中出现白屏,这意味着发生了错误。 but when i comment it //include "Helper.php";但是当我评论它时//包括“Helper.php”; and //$this->helper = new Helper();和 //$this->helper = new Helper(); the code is work fine.代码工作正常。

Is there in OOP restriction to extend Helper.php in Unicode.php and Detection.php, that Detection.php extend Unicode.php Is there in OOP restriction to extend Helper.php in Unicode.php and Detection.php, that Detection.php extend Unicode.php

On first viewing, it looks like you are getting an error for re-declaring the class 'Helper'.在第一次查看时,您似乎在重新声明 class 'Helper' 时遇到错误。

You don't need to include 'Helper.php' in 'Unicode.php' in your example above, because you are already including it in 'Detection.php'.在上面的示例中,您不需要在“Unicode.php”中包含“Helper.php”,因为您已经在“Detection.php”中包含了它。 If you need to make sure that it is included, you can use include_once() , and PHP will make sure that the file is included only on the first call.如果您需要确保它被包含,您可以使用include_once() ,并且 PHP 将确保仅在第一次调用时包含该文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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