Coding standards - inclusion

View: New views
4 Messages — Rating Filter:   Alert me  

Coding standards - inclusion

by Eric Bauman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi there,

 From http://pear.php.net/manual/en/standards.including.php: "Anywhere
you are unconditionally including a class file, use require_once.
Anywhere you are conditionally including a class file (for example,
factory methods), use include_once."

Out of curiosity, why is this the case? What benefits does it offer?

Best regards,
Eric

--
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Coding standards - inclusion

by Christian Weiske :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Eric,

>  From http://pear.php.net/manual/en/standards.including.php:
> "Anywhere you are unconditionally including a class file, use
> require_once. Anywhere you are conditionally including a class file
> (for example, factory methods), use include_once."
>
> Out of curiosity, why is this the case? What benefits does it offer?

A failed require leads to a fatal error which is not catchable by code.
In you library, you should include a file and throw an exception if
that failed - this is programmatically verifiable.


--
Mit freundlichen Grüßen
Christian Weiske

E-Mail: christian.weiske@...

Netresearch GmbH & Co. KG
-----------------------------------------------
Nonnenstraße 11d - 04229 Leipzig
Telefon: (0341) 47 842 - 20
Telefax: (0341) 47 842 - 29
-----------------------------------------------
http://www.netresearch.de - info@...

++++++++++++++
Netresearch - Spezialagentur für TYPO3 und Magento


++++++++++++++
TYPO3 Anwendertag -  02.11.2009 - http://www.typo3-anwendertag.de
Meet Magento 02/09 - 02.11.2009 - http://www.meet-magento.de 

-----------------------------------------------
Registergericht:
Amtsgericht Leipzig HRA 15614

Komplementär:
Netresearch Beteiligungs GmbH, Amtsgericht Leipzig HRB 17018

Geschäftsführer:
Michael Ablass, Thomas Fleck

--
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Coding standards - inclusion

by Eric Bauman :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

On 13/10/2009 7:54 PM, Christian Weiske wrote:

> Hello Eric,
>
>>    From http://pear.php.net/manual/en/standards.including.php:
>> "Anywhere you are unconditionally including a class file, use
>> require_once. Anywhere you are conditionally including a class file
>> (for example, factory methods), use include_once."
>>
>> Out of curiosity, why is this the case? What benefits does it offer?
>
> A failed require leads to a fatal error which is not catchable by code.
> In you library, you should include a file and throw an exception if
> that failed - this is programmatically verifiable.
>
>

Thank-you for the response, and good point.

Is there some recommended way of checking success? I thought of this:

if (@include 'test.php')
        echo 'yes';
else
        throw new Exception('no');

Some say the error control operator is quite slow; is there some
advantage to the above vs:

if (file_exists('test.php'))
        require 'test.php';
else
        throw new Exception('no');


Cheers,
Eric

--
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: Coding standards - inclusion

by Christian Weiske :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello Eric,


> >>    From http://pear.php.net/manual/en/standards.including.php:
> >> "Anywhere you are unconditionally including a class file, use
> >> require_once. Anywhere you are conditionally including a class file
> >> (for example, factory methods), use include_once."
> >>
> >> Out of curiosity, why is this the case? What benefits does it
> >> offer?
> >
> > A failed require leads to a fatal error which is not catchable by
> > code. In you library, you should include a file and throw an
> > exception if that failed - this is programmatically verifiable.
>
> Thank-you for the response, and good point.
>
> Is there some recommended way of checking success? I thought of this:
>
> if (@include 'test.php')
> echo 'yes';
> else
> throw new Exception('no');

I would not do that - especially during development, a syntax error in
test.php would be silenced and you'd stare at a white page without
knowing anything.

> Some say the error control operator is quite slow; is there some
> advantage to the above vs:
>
> if (file_exists('test.php'))
> require 'test.php';
> else
> throw new Exception('no');
That one does not work with files in your include path.

I'd include the file without any file_exist checks. If you really need
to know if your file is includable, use fopen with the
search-include-path option on (3rd parameter).

After inclusion, you can do a class_exists check to see if the class
you expected is available.


--
Mit freundlichen Grüßen
Christian Weiske

--
PEAR Development Mailing List (http://pear.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php