1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Include file inside PHP class

Discussion in 'PHP' started by suwandichen13, Jun 11, 2018.

  1. #1
    i want to ask how about to include file inside PHP class itself?
    SEMrush
    
    <?php
    $db_host	= 'localhost';
    $db_usr		= 'test';
    $db_pwd		= 'testpass';
    $db_db		= 'test';
    include('function/important.php')
    ?>
    Code (markup):
    is this already correct way?
     
    suwandichen13, Jun 11, 2018 IP
    SEMrush
  2. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,110
    Likes Received:
    1,692
    Best Answers:
    239
    Trophy Points:
    515
    #2
    Uhm, you don't have a class there...
     
    deathshadow, Jun 11, 2018 IP
  3. sarahk

    sarahk iTamer Staff

    Messages:
    25,475
    Likes Received:
    3,533
    Best Answers:
    103
    Trophy Points:
    665
    #3
    It's absolutely fine but I'd be looking at why it's a separate file

    These would be ok:
    • including a simple file with database info
    • setting up a templating or "view" functionality for MVC
    • to get a new class that can then be instantiated
    And not ok:
    • adding new functionality to the class you're already in
     
    sarahk, Jun 11, 2018 IP
  4. suwandichen13

    suwandichen13 Active Member

    Messages:
    610
    Likes Received:
    17
    Best Answers:
    0
    Trophy Points:
    90
    #4
    sorry for no class there, but i just made it as an example, does the code placement correct?
     
    suwandichen13, Jun 12, 2018 IP
  5. sarahk

    sarahk iTamer Staff

    Messages:
    25,475
    Likes Received:
    3,533
    Best Answers:
    103
    Trophy Points:
    665
    #5
    show us your actual code and we can critique that.
     
    sarahk, Jun 12, 2018 IP
    deathshadow likes this.
  6. JEET

    JEET Notable Member

    Messages:
    2,621
    Likes Received:
    188
    Best Answers:
    4
    Trophy Points:
    215
    #6
    First use "require" and not include, then use full path like: require('/home/username/public_html/function/important.php');
     
    JEET, Jun 14, 2018 IP
  7. Benanamen

    Benanamen Greenhorn

    Messages:
    22
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    20
    #7
    The OP's question is an XY Problem, therefore the answers given to it are not the right answers for the real problem. You do not hard code dependencies in a class. You use Dependency Injection. The answers to Op's question would create what is known as Tight Coupling which you do not want to do.
     
    Benanamen, Jul 1, 2018 IP
  8. NetStar

    NetStar Notable Member

    Messages:
    2,360
    Likes Received:
    501
    Best Answers:
    21
    Trophy Points:
    245
    #8
    Everyone is over thinking this... He doesn't have a created class. He's misusing the term class. He wants to know how to include a PHP script from another PHP script. Using "include" and "require" would be correct.
     
    NetStar, Jul 8, 2018 IP