0

I have created the following tools class in vendors of Yii2 application:

//@vendor/saidbakr/tools/FoxText.php
namespace FoxTools;
class FoxText
{
  public static function normalise($txt)
  {
    include(dirname(dirname(__DIR__))."/I18N/Arabic.php");  

     $norm2 = new \I18N_Arabic('Normalise');        

     return $norm2->stripTashkeel($txt);

  }
}

When using FoxText::normalise() inside a loop or reusing it inside any other block of the code, I have got the following Error:

PHP Compile Error – yii\base\ErrorException

Cannot redeclare class I18N_Arabic_Normalise 1. in C:\xampp-2\htdocs\feqh-site\vendor\I18N\Arabic\Normalise.php

I tried to use include_once and define the method without static and initializing as dynamic variable

$p = new FoxTools\FoxText() then $p->normalise() but it failed too. I also tried to:

use FoxTools\FoxText;
use FoxTools\FoxText as text2;
use FoxTools\FoxText as text3;

and it succeeded but it is not useful in loop.

What I would like to mention again, ** it works fine if I used it once**

1 Answer 1

1

Use namespaces instead of include or require. This is the recommended Yii2 method for importing files. Assuming Arabic.php is in the \I18N namespace:

namespace FoxTools;
use \I18N\Arabic;
class FoxText
{
...

Alternatively, you can use require_once instead of include:

require_once(dirname(dirname(__DIR__))."/I18N/Arabic.php"); 
Sign up to request clarification or add additional context in comments.

3 Comments

The problem, indeed, is in the Arabic library that I use. It has not support of namespaces yet. so using namespacing and use is not an option
Have you considered using Yii2's I18N implementation?
I have to use the implementation of the specific library it is dedicated for Arabic and contains a lot of features supporting many Arabic text features. it can be found here

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.