[英]Class[OOP] extends Problem in PHP
我在 PHP 中有 3(三個)class
主要 Class 是檢測。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();
?>
助手.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->helper = new Helper(); 代碼工作正常。
Is there in OOP restriction to extend Helper.php in Unicode.php and Detection.php, that Detection.php extend Unicode.php
在第一次查看時,您似乎在重新聲明 class 'Helper' 時遇到錯誤。
在上面的示例中,您不需要在“Unicode.php”中包含“Helper.php”,因為您已經在“Detection.php”中包含了它。 如果您需要確保它被包含,您可以使用include_once()
,並且 PHP 將確保僅在第一次調用時包含該文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.