SitePoint Sponsor

User Tag List

Page 7 of 7 FirstFirst ... 34567
Results 151 to 174 of 174
  1. #151
    SitePoint Addict mx2k's Avatar
    Join Date
    Jan 2005
    Posts
    256
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well i've done something like that for the short term to orgranize a small code library/framework that i'm building for myself and its ok for that. this isn't the actual function, but its the basic jist of it

    PHP Code:
                 function  imports$file )
            {
                if(! 
    file_exists$file 
                 { 
                        
    // let the error handler take care of it
                        
    trigger_error('file:'$file .' does not exist 'E_USER_NOTICE);
                        return 
    false;
                 }  else {
                        return (require_once(
    $file));
                 }
            } 

  2. #152
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Location
    United Kingdom
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    Might I suggest is_readable() instead of file_exists()?

  3. #153
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I don't like to use require_once() as there is no way clean way to deal with the failure of the file fetch. If the file is not fetchable, you get an error, which not only ugly, it's a security risk.
    Oh come on, that is paranoid. If you require files that don't exist in your source code, you're just being stupid.

    Next thing you'll tell us is to do comparisons like this ($bla === true AND $bla !== false) ?

  4. #154
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Captain Proton
    Oh come on, that is paranoid. If you require files that don't exist in your source code, you're just being stupid.
    Correct, code that bad shouldn't make it onto production servers.

    Douglas
    Hello World

  5. #155
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh come on, that is paranoid. If you require files that don't exist in your source code, you're just being stupid.
    It's not a case of the file being a requirement or not. If the file is required, then of course it'll be on the server... If you make that assumption of course?

    What if the file that is required has been deleted by a third party for example, or a sector of the disk is void due to a disk error?

    That isn't being paranoid, it's about being careful of how to handle an error which is fatal in most cases. I'm not talking about how best to present this error, or how it's logged, but how to handle it in the most efficient manner, without introducing bloat.

    That is why I don't like using require_once( ... ), as there is absolutely nothing that you can do with it, that is actually useful to the developer. If it find the file, great, if not you get an error and nothing more

    Apart from using @ to mask the error, how else could you use this function I ask? Just how elegant is your solution, since your being cynical?

  6. #156
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How about this:

    PHP Code:
    include_once 'my_file.php'
    And some generic error handling using set_error_handler?

    Later,
    Douglas
    Hello World

  7. #157
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What if the file that is required has been deleted by a third party for example, or a sector of the disk is void due to a disk error?
    That falls under my definition of paranoid.

    If the error is fatal anyhow (a fatal error by definition cannot be handled), it doesn't matter how you handle it.

    Just how elegant is your solution, since your being cynical?
    A simple require_once for files which I know to exist and a file_exist() check for files that have a dynamic file name.
    Last edited by Captain Proton; Jul 29, 2005 at 14:38. Reason: what's with the [/quoted] instead of [/quote]... :)

  8. #158
    SitePoint Addict
    Join Date
    May 2005
    Posts
    255
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Full blown packaging systems are really unnecessary for php. A simple solution like require_once('SomeFile.php','MyNameSpace'); or
    PHP Code:
    namespace Foo
    {
        class 
    Bar
        
    {
        }

    would be more than sufficient.

    The problem with php has never been correctly loading packages from the right place, it's been with name conflicts.

    I've taken to naming my APIs after Greek Gods (I'm not even kidding) to avoid naming conflicts. That way all I have to do is remember what a given diety is the God of to know what the class does.

  9. #159
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't now there was a god for database connections

  10. #160
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Location
    United Kingdom
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Etnu
    A simple solution like require_once('SomeFile.php','MyNameSpace');[/php]
    Assuming that every include file would contain nothing but a single class... include/require should not be tied to the namespace in any way shape or form, procedural and OO code should be able to be mixed freely without any problems.

  11. #161
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lazesharp
    Assuming that every include file would contain nothing but a single class... include/require should not be tied to the namespace in any way shape or form, procedural and OO code should be able to be mixed freely without any problems.
    What syntax would you use for a variable in a namespace?

    as in:

    PHP Code:
    namespace foo {
      
    $bar 'hello world';
    }

    foo::bar;
    foo::$bar;
    $foo::bar// ? 
    Douglas
    Hello World

  12. #162
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    a fatal error by definition cannot be handled
    Exactly... This is the point that I'm trying to put across. This is something that justs kills your script off, and I feel that we as developers should be able to to something before the final nail is hammered into the coffin

    I'd go with this one,

    PHP Code:
    foo::$bar
    Since it appears your looking at a class parameter no?

  13. #163
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just a normal variable in another namespace
    Hello World

  14. #164
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Location
    United Kingdom
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    What syntax would you use for a variable in a namespace?

    as in:

    PHP Code:
    namespace foo {
      
    $bar 'hello world';
    }

    foo::bar;
    foo::$bar;
    $foo::bar// ? 
    Douglas
    Personally, I'd find
    PHP Code:
    foo:$bar 
    to be the most suitable because it looks alot like the syntax for a property of a class.

    Also, you quoted me in that post, variables in namespaces have nothing to do with the point I was making, if you misunderstood, i'll put it differently. Include/require should not be synonymous with import. It is and should always be solely for including code from an external file, whether that file contains a class, a namespace or plain old procedural code is up to the author.

  15. #165
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Exactly... This is the point that I'm trying to put across. This is something that justs kills your script off, and I feel that we as developers should be able to to something before the final nail is hammered into the coffin
    Quote Originally Posted by http://www.php.net/manual/en/function.include.php
    The two constructs [include and require] are identical in every way except how they handle failure. include() produces a Warning while require() results in a Fatal Error. In other words, use require() if you want a missing file to halt processing of the page.
    I don't understand why you don't just use include_once, then. include_once produces a Warning, allowing you to gracefully do as you please.

  16. #166
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    include and include_once both return true on success if you want to test for that.
    Hello World

  17. #167
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by MiiJaySung
    Had it been anyone else, I wouldn't have mentioned Ruby seeing as it looks like it turned into a swear word here (Maybe the the moderators will replace it with stared '*'s for us )
    Here's an interesting article on that: What You Can't Say
    Hello World

  18. #168
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's an interesting read. I guess it means speaking about other languages in this forum is a taboo

  19. #169
    throw me a bone ... now bonefry's Avatar
    Join Date
    Nov 2004
    Location
    Romania
    Posts
    848
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by DougBTX
    Here's an interesting article on that: What You Can't Say
    Very nice article Speaking about other languages (especially Ruby) became taboo after a long series of "Ruby has namespaces PHP doesn't" and things like that. It's ok but within limits (i.e. without getting religios about it). And after that limit, there is a place on this forum were you can talk freely about Ruby and it's called "Perl, Python and Other Languages"

  20. #170
    SitePoint Addict chiefmonkey's Avatar
    Join Date
    Aug 2002
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The lack of namespaces is one of my major gripes about PHP5. However I subscribe to the PHP internals list and it seems that it will be added in 5.1

    George
    Got Sig!

  21. #171
    SitePoint Enthusiast
    Join Date
    Jul 2005
    Location
    United Kingdom
    Posts
    86
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by chiefmonkey
    The lack of namespaces is one of my major gripes about PHP5. However I subscribe to the PHP internals list and it seems that it will be added in 5.1

    George
    Post 5.1 actually, and that's if the patch gets approved.

  22. #172
    SitePoint Addict chiefmonkey's Avatar
    Join Date
    Aug 2002
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Lazesharp
    Post 5.1 actually, and that's if the patch gets approved.
    Bah, I must pay more attention to the mailing list. I was hoping that namespaces would be there for the new projects.

    George
    Got Sig!

  23. #173
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by bonefry
    became taboo after a long series of "Ruby has namespaces PHP doesn't"
    I doubt it, mainly because Ruby doesn't have namespaces in the first place

    Douglas
    Hello World

  24. #174
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I haven't been able to read this thread until now, but it's interesting. To hopefully make it clearer why it would be useful, a possible minimum set of requirements for a namespace system could be that it should help the following two nightmare scenarios:
    • We want to integrate two large and complex applications. Both of them use a class library containing a Form class, and the Form class is used frequently in both. So any kind of include or require from one to the other invites a name clash which is all over the place. With today's PHP, we need to change all the occurences of the class name in one of the applications, possibly in lots of different files. Even finding all of them could be a problem.
    • We want to introduce a new version of a class library, but the API has been changed. We want make the change by gradual refactoring, but that requires including both versions simultaneously as we're making the change. As it stands now, this is extremely difficult in practice because of a large number of name conflicts between the old and the new version.

    To make both of these scenarios even worse, just add type hinting.
    Dagfinn Reiersųl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •