including embedded objects rather than using external pages

Discussion in 'PHP' started by Greg-J, Apr 15, 2006.

  1. #1
    My apologies, but my programming skills revolve around solving algorithmic problems - I'm pretty new to how it all comes together.

    What would be the best way to define an object for use the way you include a page?

    Clarification:

    I have an statement that I use a lot throughout my site. Currently I have it in its own .php file and I include it in each page. I'd like to save on server calls though, and just include it in my common.php - but I'm clueless as how to do this.

    Here's a simplified form of my statement:

    <?php
    if ($handle = opendir($root . $_SERVER['REQUEST_URI'] . '/images/')) {
       $i = 1;
    	 while (false !== ($file = readdir($handle))) {
           if ($file != "." && $file != "..") {
               /* a lot of logic you're not interested in goes here */
    			 $i++;
    			 }
       }
       closedir($handle);
    }
    ?>
    PHP:
     
    Greg-J, Apr 15, 2006 IP
  2. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I am still not fully there, sorry, long working hours, my brain and thinking straight

    I think you want to include that script on each page

    You have a few to choose from
    include (will add it to page)
    require (Same as include, except if it fails, the script will die)

    require_once (Great for connection files and settings, the file has already been called it will not include it again)

    I hope this helps

    And yo have to have about 100 includes on a 100 page website with 10,000 visitors a day before worrying about how the includes effect your sever, an include is like openening another file, No big porblem.
     
    onlyican.com, Apr 16, 2006 IP
  3. Greg-J

    Greg-J I humbly return to you.

    Messages:
    1,844
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    135
    #3
    I was actually looking for a way to be able to call that code without having to include it from its own page. If not for saving on server calls, just for keeping it all tidy.

    I didn't know about the lack of strain it put on your server though. That lets me rest a little easier.
     
    Greg-J, Apr 16, 2006 IP
  4. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    There are 3 ways of having code on a page

    Include it (require, include ect)

    Manually add it in

    MySQL. (Be careful with this option,)

    MySQL queries will put more stress on the server than an include, but again not much

    look at forums, they are constantly running queries to the server, and including files.
     
    onlyican.com, Apr 16, 2006 IP
  5. Greg-J

    Greg-J I humbly return to you.

    Messages:
    1,844
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    135
    #5
    I simply want to be able to include the code (manually as you put it) in the page, but use the functionality of it by calling the code, rather than including it.

    Does this make more sense? My apologies if I'm not explaining it properly.

    Can I do this by making it a class? If so, how would I go about doing that?
     
    Greg-J, Apr 16, 2006 IP
  6. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #6
    When using include and require, it adds the code from the page you are including into your script

    If you have a page called include and on that page you had
    <? echo "Hello World"; ?>

    Then on another page you had
    <? include "include.php" ?>

    The source code in theory will be the same as include.php

    It as you wants, manually adds that page into your main page

    I good use of includes is for Navigation

    So if you add a page or change the name, you only have to change one file.
     
    onlyican.com, Apr 16, 2006 IP
  7. Greg-J

    Greg-J I humbly return to you.

    Messages:
    1,844
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    135
    #7
    I don't mean to sound ungrateful, but I'm not sure you understand what I'm talking about. I'm aware of how includes work and what their benifit is.

    I simple wanted to know the syntax to make the code above into a function or class to be called - rather than including it in each instance.
     
    Greg-J, Apr 16, 2006 IP
  8. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    Ahh i understand
    
    function HandleFunc($handle, $root, $file){
    #
    if ($handle = opendir($root . $_SERVER['REQUEST_URI'] . '/images/')) {
    #
       $i = 1;
    #
         while (false !== ($file = readdir($handle))) {
    #
           if ($file != "." && $file != "..") {
    #
               /* a lot of logic you're not interested in goes here */
    #
                 $i++;
    #
                 }
    #
       }
    #
       closedir($handle);
    #
    }
    }
    
    PHP:
    Then when you want to call it

    HandleFunc($handle, $root, $file)
    PHP:
    I hope that one helps
     
    onlyican.com, Apr 16, 2006 IP
  9. Greg-J

    Greg-J I humbly return to you.

    Messages:
    1,844
    Likes Received:
    153
    Best Answers:
    0
    Trophy Points:
    135
    #9
    Now we're getting closer...

    The only problem is that, I don't need to set the $handle, $root or $file when I call it. I just to need to call an instance of it just the way it is.

    Can I do something like this?:
    
          <?php
     function handleFunc(){
         if ($handle = opendir($root . $_SERVER['REQUEST_URI'] . '/images/')) {
             $i = 1;
               while (false !== ($file = readdir($handle))) {
                 if ($file != "." && $file != "..") {
                     /* a lot of logic you're not interested in goes here */
                       $i++;
                       }
             }
             closedir($handle);
          }
    }
          ?>
    
    PHP:
    and then call it with

    handleFunc();
    PHP:

    ?

    And thank you very much for your help.
     
    Greg-J, Apr 16, 2006 IP
  10. onlyican.com

    onlyican.com Peon

    Messages:
    206
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Then you would need to set the Vars as global

    When working in a function, it only works on vars within its own function

    $str = "Testing";
    function = ShwStr(){
    echo $str;
    }

    ShwStr

    THis will show to the browser nothing, as in the function $str does not exsists,

    You could do your function like this

    
           function handleFunc(){
    global $handle;
    global $root;
    global $file;
    
    Then it should work
               if ($handle = opendir($root . $_SERVER['REQUEST_URI'] . '/images/')) {
                   $i = 1;
                     while (false !== ($file = readdir($handle))) {
                       if ($file != "." && $file != "..") {
                           /* a lot of logic you're not interested in goes here */
                             $i++;
                            }
                   }
                   closedir($handle);
                }
          }
    
    PHP:
     
    onlyican.com, Apr 16, 2006 IP
  11. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Actually $root is the only variable that would need to be declared as global. The other two are instantiated in the function. :) And the only other part would be teh output. If the things you do in the part where you say "a lot of logic you're not interested in goes here" are internal things (nothing is printed to the screen), or you're actually doing the output there (via echo/print), then all should be well, otherwise you would just use the return statement to return the result back to your main script and when calling the function, you would do: $result = theFunction (); And you'd have the result in the $result variable.
     
    exam, Apr 16, 2006 IP