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?

    
    <?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
  2. deathshadow

    deathshadow Acclaimed Member

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

    sarahk iTamer Staff

    Messages:
    28,498
    Likes Received:
    4,460
    Best Answers:
    123
    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 Well-Known Member

    Messages:
    615
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    140
    #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:
    28,498
    Likes Received:
    4,460
    Best Answers:
    123
    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:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #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,471
    Likes Received:
    541
    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