# Required __construct call in SplFileObject

- Thread: https://externals.io/message/64156
- Original archive: https://news-web.php.net/php.internals/64156
- Started by Christoph Rosse on 2012-12-05T14:38:46+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #64156 — Christoph Rosse — 2012-12-05T14:38:46+00:00

URL: https://externals.io/message/64156#64156

Hi,

I'm just curious is there a reason why SplFileObject requires a call to 
parent::__construct()
if I override the class? Someone on my twitter feed mentioned it because 
he tried to create a Mock-Object.

The following code gives me:
PHP Fatal error:  Uncaught exception 'LogicException' with message 'The 
parent constructor was not called: the object is in an invalid state

<?php

class cls1 extends \SplFileObject
{
             public function __construct()
             {

             }


             public function `fpassthru()`
             {
             }
}
$cl = new cls1();
$cl->fpassthru();

---

## #64157 — Paul Dragoonis — 2012-12-05T14:46:56+00:00

URL: https://externals.io/message/64156#64157 · In reply to: #64156

The way this class is designed, it needs to take the file name via the
constructor as an "initiation point".

if you try to call a method of this class without initiation occurring it
will throw an exception.

this is why you must instantiate the SplFileObject's __construct() method
to start the initiation.

Hope this helps.
Paul.



> SplFileObject

---

## #64158 — Christoph Rosse — 2012-12-05T14:52:29+00:00

URL: https://externals.io/message/64156#64158 · In reply to: #64157

Maybe this should be mentioned in the docs.
I know how to use http://edit.php.net but where do I put this kind of 
information?
In the __construct method documentation 
http://php.net/manual/en/splfileobject.construct.php or somewhere else?


> The way this class is designed, it needs to take the file name via the
> constructor as an "initiation point".
>
> if you try to call a method of this class without initiation occurring it
> will throw an exception.
>
> this is why you must instantiate the SplFileObject's __construct() method
> to start the initiation.
>
> Hope this helps.
> Paul.
>
> 
>
> > […]

---

## #64159 — Paul Dragoonis — 2012-12-05T14:55:39+00:00

URL: https://externals.io/message/64156#64159 · In reply to: #64157

You could put it either inside introduction or just below it, in the 'info'
style, just to inform people they must call parent::__construct() upon
instantiation of their object in order to use this.




> The way this class is designed, it needs to take the file name via the
> constructor as an "initiation point".
>
> if you try to call a method of this class without initiation occurring it
> will throw an exception.
>
> this is why you must instantiate the SplFileObject's __construct() method
> to start the initiation.
>
> Hope this helps.
> Paul.
>
> 
>
> > […]
