我被分配到维护一个旧的PHP项目。 PHP项目使用Kohana框架。我为PHP开发人员安装了Eclipse Oxygen并创建了一个包含这些PHP代码的PHP项目。但是,我发现Eclipse Oxygen标记了大量的错误,因为无法解析对Kohana框架自动加载的类的引用。因为,Kohana利用PHP的自动加载功能并更改了类名。例如,我们在common_Core
中有一个名为common.php
的类。 Kohana自动加载了该类并将类名更改为common
。
在common.php中:
class common_Core {
public static function myFunc1() {
. . .
}
}
在client.php
中,我们只引用名为common
的此类。
$result = common::myFunc1();
Eclipse Oxygen会将common
标记为未解决,并建议更改为common_Core
。其他Eclipse版本(Mars,Neon)不会将这些标记为错误,但也不能使用Ctrl-click跳转到该方法。有没有人使用Kohana框架的自动加载功能来加载你自己的类?你如何让Eclipse解决你的课程?
如前所述,这是一个包含大量PHP代码的旧项目。因此,将所有引用从common::
更改为common_Core::
可能不是一个好的解决方案。
P.S。我只是想将类名从common_Core
更改为common
,这似乎解决了Eclipse问题。但是,除非使用名称空间,否则还存在类名与其他库冲突的风险。这个旧的PHP项目不使用命名空间。无论如何,仍然想知道Eclipse是否有办法使用Kohana框架与PHP项目一起工作。
答案 0 :(得分:0)
听起来你有奇怪的自动加载,特别是如果你有一个名为events.js:141
throw er; // Unhandled 'error' event
^
Error: listen EADDRINUSE \\.\pipe\5799b955-24fe-43fb-abe5-344be4ce5640
at Object.exports._errnoException (util.js:873:11)
at exports._exceptionWithHostPort (util.js:896:20)
at Server._listen2 (net.js:1237:19)
at listen (net.js:1286:10)
at Server.listen (net.js:1376:5)
at Object.<anonymous> (D:\home\site\wwwroot\app.js:95:7)
at Module._compile (module.js:409:26)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
npm ERR! Windows_NT 6.2.9200
npm ERR! argv "D:\\Program Files (x86)\\nodejs\\4.4.7\\node.exe" "D:\\Program Files (x86)\\npm\\2.15.8\\node_modules\\npm\\bin\\npm-cli.js" "start"
npm ERR! node v4.4.7
npm ERR! npm v2.15.8
npm ERR! code ELIFECYCLE
npm ERR! NodejsWebApp1@0.0.0 start: `node app.js`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the NodejsWebApp1@0.0.0 start script 'node app.js'.
npm ERR! This is most likely a problem with the NodejsWebApp1 package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR! node app.js
npm ERR! You can get information on how to open an issue for this project with:
npm ERR! npm bugs NodejsWebApp1
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!
npm ERR! npm owner ls NodejsWebApp1
npm ERR! There is likely additional logging output above.
npm ERR! Please include the following file with any support request:
npm ERR! D:\home\site\wwwroot\npm-debug.log
的文件中有一个名为# Setup a graph
import tensorflow as tf
placeholder0 = tf.placeholder(tf.float32, [])
placeholder1 = tf.placeholder(tf.float32, [])
constant0 = tf.constant(2.0)
sum0 = tf.add(placeholder0, constant0)
sum1 = tf.add(placeholder1, sum0)
# Function to get *all* dependencies of a tensor.
def get_dependencies(tensor):
dependencies = set()
dependencies.update(tensor.op.inputs)
for sub_op in tensor.op.inputs:
dependencies.update(get_dependencies(sub_op))
return dependencies
print(get_dependencies(sum0))
print(get_dependencies(sum1))
# Filter on type to get placeholders.
print([tensor for tensor in get_dependencies(sum0) if tensor.op.type == 'Placeholder'])
print([tensor for tensor in get_dependencies(sum1) if tensor.op.type == 'Placeholder'])
的类。您是否可以在具有命名空间的新脚本中制定更新的标准?我想你的自动加载器可以处理这个标准。
<强> /vendor/Common/Core.php 强>
common.php
<强> client.php 强>
common_Core
或者只是使用namespace Common;
class Core
{
public static myFunc()
{
}
}
可能有用吗?
# Reassign the class to "common" if need be
use \Core\Common as common;
# Assign
$result = common::myFunc();
另外,如果您看到their manual here,则会说:
当调用尚未加载的类(例如:Session_Cookie)时,Kohana将使用Kohana :: find_file搜索文件系统,找到名为classes / session / cookie.php的文件。
如果您的课程不遵循此惯例,则Kohana不能自动加载。您必须手动添加文件,或添加自己的autoload function。
答案 1 :(得分:0)
似乎Kohana框架在运行时使用eval()
为具有特定后缀的自动加载类创建别名类名称(例如common_Core
- &gt; common
,email_Core
- &gt; email
)。不确定为什么需要别名。这种做法不仅增加了名称冲突的风险(一些旧的PHP代码不使用命名空间),它混淆了Eclipse,Eclipse将这些别名命名为错误。使用eval()
,Eclipse(或任何PHP IDE)无法验证别名类名,因为它是在运行时确定的。一个简单的解决方案是删除后缀。例如,将类common_Core
重命名为common
。然后,你仍然可以使用Kohana框架而没有副作用。
Kohana.php:
. . .
. . .
if ($filename = self::find_file($type, self::$configuration['core']['extension_prefix'].$class))
{
// Load the class extension
require $filename;
}
elseif ($suffix !== 'Core' AND class_exists($class.'_Core', FALSE))
{
// Class extension to be evaluated
$extension = 'class '.$class.' extends '.$class.'_Core { }';
// Start class analysis
$core = new ReflectionClass($class.'_Core');
if ($core->isAbstract())
{
// Make the extension abstract
$extension = 'abstract '.$extension;
}
// Transparent class extensions are handled using eval. This is
// a disgusting hack, but it gets the job done.
eval($extension);
}
. . .
. . .
P.S。在详细阅读了Kohana框架之后,我发现eval()用于实现所谓的Transparent Class Extending
功能。请参阅以下两个链接:
Transparent Class Extending
:https://v2docs.kohanaframework.org/3.3/guide/kohana/extension
Cascading FileSystem
:https://v2docs.kohanaframework.org/3.3/guide/kohana/files
我发现在旧代码中使用Kohana框架是错误的。我们不应该声明任何带有_Core
后缀的类,因为它们是为Kohana核心类保留的。由于没有common_Core
Kohana类,我们应该将其命名为common
。但是,有email_Core
Kohana课程,我们应该宣布课程email
从email_Core
延伸。
在应用程序目录中:
class common {
// there is no common_Core in Kohana
}
class email extends email_Core {
// there is email_Core in Kohana
}
无论如何,我仍然认为使用eval()是不好的做法并且很危险。如果你错误地使用透明类扩展,这会使Kohana框架与任何其他PHP IDE不兼容。
发现其他类似帖子讨论同一问题: http://forum.kohanaframework.org/discussion/212/can-developers-remove-evalclass-class-extends-class-_core-/p1