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?