从文件和当前类或新的类中以编程方式构建一个类 [英] build a class programmatically from Files and current Class or in new one
本文介绍了从文件和当前类或新的类中以编程方式构建一个类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
A。我有多个这样的文件,里面的方法是这样的:
A. i have this multiple files with methods inside it like this:
file1.php
<?php
function Example1() {
echo 'Example1';
}
function Example2() {
echo 'Example1';
}
?>
B。
file2.php
<?php
function Example3() {
echo 'Example1';
}
function Example4() {
echo 'Example1';
}
?>
C。而且我想将此与一个类关联,但是我需要以编程方式使其:
C. and i want to associate this to a class but i need make it programmatically:
class Class_Example {
public function ChargeFunction() {
$DirectoryToScan ='/functionsfiles/'
$ABS = scandir(DirectoryToScan, 1);
$FunctionFiles=[];
foreach ($ABS as $key => $name) {
if (strpos($name, 'class.') !== false) {
$FunctionFiles[$name] = DirectoryToScan . $name;
}
}
foreach (FunctionFiles as $key => $functionfile) {
require_once $functionfile;
}
//all methods can be used on here.
}
}
D。我不能使用,是我想要的吗?
D. i cant use, is that i want:
$intance = new Class_Example();
$intance->Example1();
$intance->Example2();
如何在不更改结构或内容的情况下将文件与方法关联到类
推荐答案
我用以下代码解决它:
class Class_Example {
public function ChargeFunction() {
$DirectoryToScan ='/functionsfiles/'
$ABS = scandir(DirectoryToScan, 1);
$FunctionFiles=[];
foreach ($ABS as $key => $name) {
if (strpos($name, 'class.') !== false) {
$FunctionFiles[$name] = DirectoryToScan . $name;
}
}
foreach ($FunctionFiles as $key => $functionfile) {
require_once $functionfile;
}
}
function __call($functionName, $args) {
if (function_exists($functionName)) {
return call_user_func_array($functionName, $args);
}
}
}
实现魔术功能 __ call
解决此问题。
implement the magic function __call
to solve this.
这篇关于从文件和当前类或新的类中以编程方式构建一个类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文