收录日期:2019/12/09 08:30:47 时间:2010-10-28 14:26:56 标签:php,class,include

Possible Duplicate:
Can I include code into a PHP class?

Hello,

I have some third-part php files that I would like to include as classes in my application. The problem is those files keep changing and they are not OOP, just a bunch of functions. To keep them updated and to work in my framework I have to use them in a class, so I would like to include the file and access it via -> operator:

Example:

class Vendor {
    include("../vendor/functions.php");
}

and then:

$vendor = new Vendor();
$vendor->foorbar();

Any ideas ?

I think that the best way to go would be to modify (automated) the function-files to include

namespace VendorName;

That way the global namespace is not polluted but you have access to the functions. Calls my be performed by:

VendorName\FunctionName($arguments);

I presume that those third-party files are not going to be injected into your app in any automated fashion and you just want to easy the task of having them available in your framework while you're able to upgrade to newer versions. In such case, I recommend you use what Subversion calls "vendor branches". It's just a concept you can use it whatever your source control tool is. To sum up:

  • You download the third-party code and store it into your code repository.
  • You copy the code into your app and modify it to suit your needs.
  • When the vendor releases a new version, you update the stored code.
  • You use your version control software to copy the changes into your app.