当前位置: 动力学知识库 > 问答 > 编程问答 >

php - What is the best practice for using non-namespaced classes within namespaced class

问题描述:

I am creating a library using the wonderful new namespacing features that PHP has implemented in recent updates. However, I need to use the simple_html_dom.php class from within one of my classes. I'm using a standard PSR-0 compliant autoloader class, but I'm getting errors when trying to create a new object of the simple_html_dom class because of the underscores.

When looking at my autoloader class it is substituting underscores for whacks. This is easily fixed, but I'm afraid it might break my PSR-0 compliance. I was wondering what were the best practices to solve such an issue with legacy libraries.

Directory Structure

- vendor/

- MyStuff/

- lib/

- simple_html_dom.php

- MyClass1.php

MyClass1.php

<?php

namespace MyStuff;

// This require works

require(__DIR__ . '/lib/simple_html_dom.php');

class MyClass1 {

public function __construct() {

// This breaks

// Warning: require(C:\www\vendor\LoU\simple\html\dom.php): failed to open stream

$html = new simple_html_dom();

}

}

网友答案:

Giving this - self advertising - answer just because you told me to do it. :)

You can use my autoloader class: Jm_Autoloader. (you told in comments that it worked for you)

It is designed to work with OLD_SCHOOL_CLASS_NAMES as well as \new\school\class\Names and both styles mixed together.

Installation is easy using PEAR:

pear channel-discover metashock.de/pear
pear channel-update metashock
pear install metashock/Jm_Autoloader

On top of your main php script just add:

require_once 'Jm/Autoloader.php';

Thats it in most cases. If not, there are some options I'll not describe here as it not belongs here.

Have fun!

分享给朋友:
您可能感兴趣的文章:
随机阅读: