drsqpko5286 2016-01-27 19:35 采纳率: 0%
浏览 18
已采纳

在PHP类中使用实用程序函数的最佳实践是什么? [关闭]

I'm coming into object-oriented PHP from a more functional Javascript background, so I'm fairly new to both PHP and OOP.

What I'm trying to do is make a class that that is passed the contents of a text file, divides it up into manageable pieces, and then loops over those pieces (steps), and handles them accordingly, sometimes making HTTP requests, sometimes saving chunks of info into a $fileVariables array. Here's a basic outline of what I have:

class Script {
    public $file, $client, $fileVariables;

    function __construct($file) {
        $this->file = $file;
        $this->client = new \GuzzleHttp\Client();
        $this->fileVariables = array();
    }

    public function parseAndRun() {
        $client = $this->client;
        $file = $this->file;
        // this is the function that performs the bulk of the class's
        // work. It relies on some utility functions that are defined
        // after, and are utilized within this function like this:
        $steps = $this->divideFileIntoSteps($file);
        // or like this:
        $this->setVariable($index, $value);
    }

    public function divideFileIntoSteps($file) {
        // return array of file divided into steps
    }

    public function setVariable($index, $value) {
        // push $index => $value into $fileVariables array
    }
}

I think it's possible that I'm trying to do something that would be totally okay in functional Javascript, but just feels sloppy in object-oriented PHP. Also, when I run PHPUnit tests on this, I get an error that's something like Serialization of 'Closure' is not allowed, which I think from doing some googling on the error might have something to do with how I'm doing this. Also, the fact that I have to use $this-> over and over again on commonly used functions makes me feel like I'm doing something wrong. It just gives me that bad code feeling.

Is there a better way to do this, bearing in mind that a lot of the functionality has to do with getting and setting items in the $fileVariables array?

  • 写回答

1条回答 默认 最新

  • dsbgltg159136540 2016-01-27 20:19
    关注

    Your code is basically correct.

    You don't need $file = $this->file;

    $this prefix is standard for accessing member functions and variables in PHP.

    Your function could look like this:

    public function parseAndRun() {
        $steps = $this->divideFileIntoSteps($this->file);
        // or like this:
        $this->setVariable($index, $value);
    }
    

    Another best practice is to declare private all methods that you don'n need to access from outside the class. Member variables should all be private and you should have accessor functions for them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。