Parsing glider data from IGC files I have to do a program in PHP that reads IGC files like this IGC file and gets records about the glider.
For now I came up with something like this:
&lt;?php

        class Pilot {
            public $name;
            public $gliderType;
            public $competitionId;
            public $gpsDatuml;
            public $competitionClass;
            public $startPoint;
            public $endPoint;
        }
        $pilot = new Pilot();
        $myFile = new SplFileObject(&quot;https://xcportal.pl/sites/default/files/tracks/2020-06-09/069daro396091568.igc&quot;, 'r', 10);
        for($i=0; $i &lt; 10; $i++) {
            $information =  $myFile-&gt;fgets();
            if(($pos = strpos($information, &quot;PILOT&quot;)) !== FALSE){
                $pilot-&gt;name = substr($information, $pos+7);
            }
            if(($pos = strpos($information, &quot;GLIDERTYPE&quot;)) !== FALSE){
                $pilot-&gt;gliderType = substr($information, $pos+12);
            }
            if(($pos = strpos($information, &quot;COMPETITIONID&quot;)) !== FALSE){
                $pilot-&gt;competitionId = substr($information, $pos+15);
            }
            if(($pos = strpos($information, &quot;GPSDATUM&quot;)) !== FALSE){
                $pilot-&gt;gpsDatuml = substr($information, $pos+10);
            }
            if(($pos = strpos($information, &quot;CLASS&quot;)) !== FALSE){
                $pilot-&gt;competitionClass = substr($information, $pos+7);
            }
            // echo strpbrk($information, &quot;PILOT&quot;);
            // echo substr($information, 10);
        }
        echo $pilot-&gt;name;
        echo $pilot-&gt;gliderType;
        echo $pilot-&gt;competitionId;
        echo $pilot-&gt;gpsDatuml;
        echo $pilot-&gt;competitionClass;
    
        $myFile = null;
    ?&gt;

but I wonder if there is anyway to do it better that I haven't figured out already.
Also is it good practice to keep properties of the class as a private field or can I leave them as public if any other class doesn't use that property?
I don't think I understand the point of declaring a class if you aren't going to write any methods in it. I mean why go the expense of declaring a class just to store some variables outside of the global scope.  Assuming this class can never populate its properties without parsing a document, so it make sense to have its constructor initiate the file reading and generate values for the properties.

I haven't personally used SPLFileObject before, but the manual says that:

the 2nd parameter $open_mode defaults to r and
the 3rd parameter $use_include_path expects a boolean value.  I expect that that 10 is not doing what you intend.


Reading a limited number of lines from the file with fgets() is a professional decision that shows real care and deliberate scripting.  Removing all debate regarding micro-optimization or if reading the whole file is an &quot;affordable&quot; cost, I love that you are endeavoring to ONLY &quot;read what you need&quot;.

By my count, there are 3 factors which should halt the loop of reading of lines:

there are no more lines in the file
a line is encountered that does not contain a colon
you have extracted all relevant details from the file up to your self-imposed &quot;magic counter&quot; 10

The first two logical break points should be baked into the loop's breaking syntax.  The third is probably something that you don't want to get into the habit of.  Another developer may look at the code (without seeing a sample input file) and think: &quot;Why is this operation limited to 10? Why not 20?  Why not 5?&quot;  It is best to avoid magic numbers, or if you need to have them, write a comment explaining why it is implemented.

That battery of if conditions is considerably less wise.  Think about it.  If the first if is satisfied -- why would you want to bother executing the other four checks?  They should never be satisfied if the first one is.  While I am going to suggest a different technique in my snippet to follow, your snippet at the very least would be improved by using subsequent elseif conditions.


Because I'm rather familiar with regex, I find using regex with a lookup array to be a very convenient technique to extract your targeted values from the remote file.  Not only does regex afford a more compact script, it maintains the flexibility of your original script by allowing the targeted lines to exist in any order.
A code suggestion:
class Pilot
{
    public $name;
    public $gliderType;
    public $competitionId;
    public $gpsDatuml;
    public $competitionClass;
    public $startPoint;  // I don't know where/how this gets populated
    public $endPoint;  // I don't know where/how this gets populated

    private $keywords = [
        'PILOT' =&gt; 'name',
        'GLIDERTYPE' =&gt; 'gliderType',
        'COMPETITIONID' =&gt; 'competitionId',
        'GPSDATUM' =&gt; 'gpsDatuml',
        'CLASS' =&gt; 'competitionClass',
    ];
    
    public function __construct(string $url)
    {
        $this-&gt;populatePropertiesFromUrl($url);        
    }
    
    public function populatePropertiesFromUrl(string $url): void
    {
        $pattern = '~(' . implode('|', array_keys($this-&gt;keywords)) . '): (.*)~';

        $file = new SplFileObject($url);
        while (!$file-&gt;eof()
            &amp;&amp; ($line = $file-&gt;fgets())
            &amp;&amp; strpos($line, ':') !== false
        ) {
            if (preg_match($pattern, $line, $match)) {
                $this-&gt;{$this-&gt;keywords[$match[1]]} = $match[2];
            }
        }
    }
}
$pilot = new Pilot('https://xcportal.pl/sites/default/files/tracks/2020-06-09/069daro396091568.igc');
var_export(get_object_vars($pilot));

Output (should be):
array (
  'name' =&gt; 'DARIUSZ KISZKO',
  'gliderType' =&gt; 'ADVANCE IOTA 2',
  'competitionId' =&gt; '0000',
  'gpsDatuml' =&gt; 'WGS-84',
  'competitionClass' =&gt; 'Paraglider (Standard)',
  'startPoint' =&gt; NULL,
  'endPoint' =&gt; NULL,
)

*Disclaimer: I am making some assumptions about the text in the remote file.  I don't actually know how the text may vary.  I don't know if my suggested code will hold up against other files.  You may wish to adjust my loop breaking algorithm.