문제

I know there is tidy php class by default already, what about if I want to create my own custom tidy class and I want to name that class as tidy too?

class tidy
{
    public function html()
    {
        return 'local';
    }
}

$tidy = new tidy();
echo $tidy->html();

error,

Fatal error: Cannot redeclare class tidy in C:\wamp\www\test\2012\php\dom_tidy_html_8.php on line 11

How can I not to conflict with PHP's default tidy?

Is it possible?

EDIT:

I tested it with namespace and it works but I am not whether this is a good practice or not...? I haven't done anything with namespace before so I am not sure whether it is safe or not...

namespace tidy;

class tidy
{
    public function html()
    {
        return 'local';
    }
}

$tidy = new tidy();
echo $tidy->html();

Would it cause any problem if I want to use the PHP default tidy occasionally?

도움이 되었습니까?

해결책

If you want to build on top of tidy then just extend the tidy class and call is myTidy.

class myTidy extends tidy
{
    public function html()
    {
        return 'local';
    }
}

$tidy = new myTidy();
echo $tidy->html();

If you want to create your own implementation from scratch try to remove the tidy module and then create the tidy class.

If you are using PHP >= 5.3.0 you can also create your own tidy class in a Namespace :

namespace My\Tidy;

class tidy
{
    public function html()
    {
        return 'local';
    }
}

$tidy = new tidy();
echo $tidy->html();

다른 팁

Just name it something different:

class my_tidy
{
    public function html()
    {
        return 'local';
    }
}

$tidy = new my_tidy();
echo $tidy->html();
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top