【问题标题】:Phalcon : How to access global function?Phalcon:如何访问全局函数?
【发布时间】:2014-01-24 07:27:03
【问题描述】:

我对使用 phalcon 完全陌生。
1. 我创建了一个名为 library
的目录 2. 在那个目录中,我创建了一个名为 commonfunctions.php 的文件
3. 我写了一个

abstract class Commonfunctions extends Phalcon\Mvc\User\Component
{
    Public function ProfileImage() //function to set profile pic
    {
       $auth = $this->session->get('auth');
       $user_id = $auth['id'];     //get session id
       $TblPersonaldetails = TblPersonaldetails::findFirst("user_id = ".$user_id);
       echo "pic = ".$profile_pic = $TblPersonaldetails->profile_pic;  //get profil_pic path from db
    }
}

但我收到来自“通知:在第 98 行访问 D:\xampp\htdocs\test\app\library\Commonfunctions.php 中的未定义属性 TblPersonaldetails::profile_pic”的通知消息

我该如何解决这个问题?

【问题讨论】:

  • 你确定$TblPersonaldetails 有属性profile_pic 吗?
  • 您可以执行 var_dump($TblPersonaldetails->toArray()) 并查看所有属性。如果您设置了自定义 columnMap(例如 profile_pic => profilePic),请改用 $TblPersonaldetails->profilePic。
  • 是的 TblPersonaldetails 有属性 profile_pic
  • 感谢 sectus 和 dompie 的即时回复我得到了解决方案,使用 $profilePic = $TblPersonaldetails->getProfilePic();这段代码我的问题得到解决
  • 以后我建议始终将模型中的所有字段添加为私有变量和配置集/获取器以设置/获取您的值。这样,您将随时准备添加数据验证,轻松进行更改,并且永远不会像现在这样面对此类错误。你应该始终使用 setter 和 getter

标签: php function class phalcon


【解决方案1】:

这可能就是你要找的。​​p>

Service 创建一个名为Service 的文件夹,然后将您的Commonfunctions 移动到新创建的Service 文件夹中。在您的 Commonfunctions 文件中添加了 namespace *Service*

在您的 config.php 或 loader.php 中执行类似

$loader->registerNamespaces(
    array(
        "Service" => $config->application->servicesDir,
    )
);

您的配置应该将 servicesDir 设置为 app/services

然后,如果所有加载成功,您可以像这样调用您的方法“ProfileImage”

$app->getDI()->get('Service\Commonfunctions')

希望对你有帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多