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

时间:2011-08-14 17:20:55

标签: php oop

我在PHP中有3(3)个课程

主要类是Detection.php

Detection.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
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

<?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
    }
.................
}
?>

在Unicode.php中你可以看到有两行用双斜杠'//'评论。当我删除并运行代码时,我在浏览器中显示白屏,这意味着发生了错误。但当我发表评论//包括“Helper.php”;和// $ this-&gt; helper = new Helper();代码工作正常。

是否存在OOP限制以扩展Unicode.php和Detection.php中的Helper.php,即Detection.php扩展Unicode.php

1 个答案:

答案 0 :(得分:4)

在第一次观看时,看起来您在重新声明班级“助手”时遇到错误。

在上面的示例中,您不需要在“Unicode.php”中包含“Helper.php”,因为您已将其包含在“Detection.php”中。如果您需要确保包含它,可以使用include_once(),PHP将确保该文件仅包含在第一次调用中。