SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast menkes's Avatar
    Join Date
    Jul 2002
    Location
    Channel Islands
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Declare Globals Inside Function

    I am converting a small perl application I developed (run from the command line) to a PHP web application. This "application" is a function in an include file, and this function will call about 30+ other functions including a couple perl file parsers.

    The problem I have is that all of my global vars will be defined within that first function. I will need ready access to those vars in all subsequent functions. Being an efficient (aka, lazy) programmer, I am looking for ideas on how to do this with the least amount of code.

    Thoughts?
    Hope this helps...

  2. #2
    The Hiding One lynlimz's Avatar
    Join Date
    Jul 2000
    Location
    Singapore
    Posts
    2,103
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Have you considered using PHP classes?
    http://www.php.net/manual/en/ref.classobj.php
    "Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
    -- Albert Einstein

  3. #3
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Two ways:

    1.

    A class

    PHP Code:
    class Something;

    function 
    one()
    {
    $this->variable "this";
    }

    function 
    two()
    {
    echo 
    $this->variable;
    }


    Or:

    PHP Code:
    function one()
    {
    $somevar "this";
    }

    function 
    two()
    {
    GLOBAL 
    $somevar
    echo $somevar;

    If yiu know OOP, go iwht the first one, if you don't, use globaling.

  4. #4
    SitePoint Enthusiast menkes's Avatar
    Join Date
    Jul 2002
    Location
    Channel Islands
    Posts
    75
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the responses. I was thinking a class was the way to go....
    Hope this helps...


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
  •