0

I have 3(three) class in PHP

main Class is 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
    }
.................
}
?>

In Unicode.php as you can see there is two line that i comment with double slashes '//'. 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"; and //$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

7
  • Your constructor is doing work. Refactor it Commented Aug 14, 2011 at 17:30
  • Detection.php is a file, not a class. Commented Aug 14, 2011 at 18:33
  • 1
    @Gordon: Nonsense. RAII, dude! Commented Aug 14, 2011 at 18:34
  • @Tomalak I cant find the irony tags?! Commented Aug 14, 2011 at 19:56
  • @Tomalak Geret'kal : why ? can you explain. Thanks Commented Aug 15, 2011 at 3:40

1 Answer 1

4

On first viewing, it looks like you are getting an error for re-declaring the 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'. 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.

Sign up to request clarification or add additional context in comments.

2 Comments

I Change include "Helper.php"; to include_once("Helper.php"); and its work, but what's the difference ?
@Ahmad: Look include up in the documentation, then look up include_once in the documentation, and compare the difference yourself.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.