Question

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?

Était-ce utile?

La solution

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();

Autres conseils

Just name it something different:

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

$tidy = new my_tidy();
echo $tidy->html();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top