dtch60248 2010-04-22 19:03
浏览 12
已采纳

通过$ this->从一个名为静态的函数访问当前类。

This feels a bit messy, but I'd like to be able to call a member function statically, yet have the rest of the class behave normally...

Example:

<?php
class Email
{
    private $username = 'user';
    private $password = 'password';
    private $from     = 'test@example.com';
    public  $to;

    public function SendMsg($to, $body)
    {
        if (isset($this))
            $email &= $this;
        else
            $email = new Email();

        $email->to = $to;

        // Rest of function...
    }
}

Email::SendMsg('mqa@test.com');

How best do I allow the static function call in this example?

Thanks!

  • 写回答

3条回答 默认 最新

  • dongxiejie9387 2010-04-22 21:00
    关注

    So basically you want the static method to be a "shortcut" for:

    $mail = new Email();
    $mail->to = 'somebody@somewhere.com';
    $mail->body = 'this is the body';  // this property was not in your example, but assuming...
    $mail->Send();
    

    Perhaps:

    // class declaration omitted ...    
    
    static public function SendMsg( $to, $body )
    {
        $mailobject = new self;
    
        $mailobject->to = $to;
        $mailobject->body = $body;
        $mailobject->Send();
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 seatunnel 怎么配置Elasticsearch
  • ¥15 PSCAD安装问题 ERROR: Visual Studio 2013, 2015, 2017 or 2019 is not found in the system.
  • ¥15 (标签-MATLAB|关键词-多址)
  • ¥15 关于#MATLAB#的问题,如何解决?(相关搜索:信噪比,系统容量)
  • ¥500 52810做蓝牙接受端
  • ¥15 基于PLC的三轴机械手程序
  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程