php - 如何为使用自定义函数实例化的类添加自动完成功能?
问题描述
我通过自定义函数实例化我的类。
这些类在app/code/core/例如
app/code/core/Property/Helper/Property.php
require_once("Core/Helper.php");
class Property_Helper_Property extends Core\Helper
{
public function __construct($con)
{
parent::__construct($con);
}
public function test()
{
return "hello";
}
}
应用程序/代码/核心/核心/Helper.php
<?php
namespace Core;
abstract class Helper
{
protected $con;
public function __construct($con)
{
$this->con = $con;
}
}
我可以从任何文件中调用test()
类的方法,如下所示:Property_Helper_Property
require_once 'app/Main.php'; // always needed
Main::getHelper("Property/Property")->test();
应用程序/Main.php
该文件包含最终类“Main”,它具有静态方法 getHelper
if (!defined('DS')) define('DS', DIRECTORY_SEPARATOR);
if (!defined('PS')) define('PS', PATH_SEPARATOR);
if (!defined('BP')) define('BP', dirname(dirname(__FILE__)));
/**
* Set include path
*/
Main::register('original_include_path', get_include_path());
$paths = array();
$paths[] = Main::CORE_PATH;
$paths[] = Main::LOCAL_PATH;
$paths[] = BP . DS . 'lib';
$paths[] = BP . DS . 'inc';
$appPath = implode(PS, $paths);
set_include_path($appPath . PS . Main::registry('original_include_path'));
final class Main
{
const CORE_PATH = BP . DS . 'app' . DS . 'code' . DS . 'core';
const LOCAL_PATH = BP . DS . 'app' . DS . 'code' . DS . 'local';
/**
* Registry collection
*
* @var array
*/
static private $_registry = array();
public static function getDbConnection()
{
return self::registry("db_connection");
}
/**
* Register a new variable
*
* @param string $key
* @param mixed $value
* @param bool $graceful
*/
public static function register($key, $value, $graceful = false)
{
if (isset(self::$_registry[$key])) {
if ($graceful) {
return;
}
self::throwException('Main registry key "'.$key.'" already exists');
}
self::$_registry[$key] = $value;
}
/**
* Unregister a variable from register by key
*
* @param string $key
*/
public static function unregister($key)
{
if (isset(self::$_registry[$key])) {
if (is_object(self::$_registry[$key]) && (method_exists(self::$_registry[$key], '__destruct'))) {
self::$_registry[$key]->__destruct();
}
unset(self::$_registry[$key]);
}
}
/**
* Retrieve a value from registry by a key
*
* @param string $key
* @return mixed
*/
public static function registry($key)
{
if (isset(self::$_registry[$key])) {
return self::$_registry[$key];
}
return null;
}
public static function getHelper($name)
{
$classPath = self::getClassPath($name, "Helper");
if (!$classPath) { return false; }
$fullClassPath = self::getFullClassPath($classPath);
if (!$fullClassPath) { return false; }
$obj = self::getClassInstance($fullClassPath, $classPath);
if (!$obj) { return false; }
return $obj;
}
public static function getModel($name)
{
$classPath = self::getClassPath($name, "Model");
if (!$classPath) { return false; }
$fullClassPath = self::getFullClassPath($classPath);
if (!$fullClassPath) { return false; }
$obj = self::getClassInstance($fullClassPath, $classPath);
if (!$obj) { return false; }
return $obj;
}
private function getClassInstance($fullClassPath, $classPath)
{
if (!$classPath) { return false; }
require_once($fullClassPath);
$className = str_replace("/", "_", $classPath);
if (class_exists($className)) {
return new $className(self::getDbConnection());
} else {
return false;
}
}
private function getFullClassPath($classPath)
{
$modulPaths = [self::CORE_PATH, self::LOCAL_PATH];
return self::checkIfFileExistInModule($modulPaths, $classPath);
}
private function getClassPath($modelName, $identifier="Model")
{
if (strpos($modelName, '/') === false) { return false; }
if (substr_count($modelName, "/") == 1) {
$exp = explode("/", $modelName);
return $exp[0] . "/$identifier/" . $exp[1];
} else {
return false;
}
}
private function checkIfFileExistInModule($modulPaths, $modelname)
{
foreach($modulPaths as $path) {
$path = $path . DS . $modelname . ".php";
if (file_exists($path)) {
return $path;
}
}
return "0";
}
}
这工作得很好......现在是实际的问题。
如果我在写...
$obj = Main::getHelper("Property/Property");
$obj->
...然后我的 IDE (NetBeans) 不会自动建议我可以使用的公共方法/属性。
有没有办法“教”我的逻辑来添加自动建议/自动完成,以便它自动显示对象中可用的所有公共方法/属性?
解决方案
您需要使用phpdoc 块。很确定它们受 NetBeans 支持:
/** @var Property_Helper_Property $obj */
$obj = Main::getHelper("Property/Property");
从那时起,自动完成和静态分析将起作用,因为可以理解这$obj
将是Property_Helper_Property
.
推荐阅读
- python - 在 PyTorch 中保存模型的框图
- mysql - 试图将这些结合在一起,但我不知道它告诉我什么:将数据类型 varchar 转换为 bigint 时出错
- node.js - 如何在插入之前对密码进行哈希处理?
- firebase - 如何在不使用 Firestore 的情况下使用 FCM 向用户设备推送通知
- powershell - Powershell - 在电子邮件上调用 Move 函数会导致输出极其冗长
- python - 为什么我不能对此子流程模块方法返回的“字符串”执行正则表达式?
- gsuite-addons - 与外部用户共享 gsuite 插件而不发布到 Marketplace 的正确方法是什么?
- java - 使用java流替换嵌套循环连接字符串的最佳方法
- c# - 如何与传入消息进行字符串比较
- angular - ZEIT Now Angular PWA 手柄 404