Selectively including classes - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Selectively including classes

ITAITA Posts: 463Member
I'm currently working on a project which contains many classes. Each class is written in a separate file. Each file which I work on has included classes in it. Some classes rely on others and those classes also include files. What I want to do is to stop receiving the "cannot redeclare class" error. Is there any way of doing this? (I can get rid of them, but I have to work out file dependencies and it's slooow).

I'm thinking along the lines of the C++ '#ifndef and #include' directives to stop multiple includes.

If this is not possible then, is there any way in which this can be done by writing a little code in PHP to check to see if the file has already been included by PHP and if so prevent it from being included again.

Please help, (otherwise I'll have to spend ages trying to code round the problem :-( )

Many thanks,

ITA
"Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"

Comments

  • netgertnetgert Posts: 331Member
    : I'm currently working on a project which contains many classes. Each class is written in a separate file. Each file which I work on has included classes in it. Some classes rely on others and those classes also include files. What I want to do is to stop receiving the "cannot redeclare class" error. Is there any way of doing this? (I can get rid of them, but I have to work out file dependencies and it's slooow).
    :
    : I'm thinking along the lines of the C++ '#ifndef and #include' directives to stop multiple includes.
    :
    : If this is not possible then, is there any way in which this can be done by writing a little code in PHP to check to see if the file has already been included by PHP and if so prevent it from being included again.
    :
    : Please help, (otherwise I'll have to spend ages trying to code round the problem :-( )
    :
    : Many thanks,
    :
    : ITA
    : "Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"
    :

    instead of include() use include_once() - this will not re-include an already included file
    alteratively, you could write one classloader script that has if's and else's to decide which classes you want to load
    if you're using PHP5 you might also want to check out how to autoload objects,
    http://ee.php.net/manual/en/language.oop5.autoload.php
    [hr][red][italic][b]N[/b][/red][blue]et[/blue][red][b]G[/b][/red][blue]ert[/italic][/blue][hr]

  • ITAITA Posts: 463Member
    thx a million. No more deps!

    ITA


    "Let us smite the evil slime eating hordes who may befall us on our quest to be the ultimate programmers of the known universe!"

Sign In or Register to comment.