SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Classes and Functions question.

    Hello!

    Could someone tell me why a class that is instantiated outside of a function cannot be used inside the function?

    An example
    PHP Code:
    // Instantiate new Object from the Class Smarty
    $smarty = new Smarty;

    function 
    ShowCompanyDetails()
    {
        
    // I can't use the $smarty in here, if i want to   use the class in here i have to instantiate it again.

        // This will NOT work.
        
    $smarty->assign("whatever""Text bla bla");
    }
    // This will work.
    $smarty->assign("whatever""Text bla bla"); 
    So basically this forces me to instantiate two Objects of the class.

    Why? Creating two objects forces me to use more memory which is a waste IMHO (even if it aint really noticable).

    So, is there any way i can use a class created outside of a function in a function?

    Thanks a lot in advance,
    datune

  2. #2
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    simply declare the class object as global within the function

    PHP Code:
    $smarty = new Smarty;

    function 
    ShowCompanyDetails()
    {
        global 
    $smarty;
        
        
    //works now!
        
    $smarty->assign("whatever""Text bla bla");
    }

    //works still
    $smarty->assign("whatever""Text bla bla"); 

  3. #3
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, i see, but i have read quite a lot of rants on this forums as to not use globals. I have no idea why not though, maybe someone cares to tell me?

    Thanks rect

  4. #4
    SitePoint Enthusiast
    Join Date
    Apr 2002
    Posts
    90
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    the are referring to the register_globals directive most probably. the php help has more info and like you said there are lots of threads discussing it. as far as globals for functions, there isn't a way out of it that i see. like you said you should be able to use the same class object throughout all your code, so using the global declaration is the only way.

  5. #5
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, yeah you are probably right, mind you i know all about register_globals, but im not sure if they didn`t mean globals in general.

    Well i will try to look it up, on the other hand like you said, there is no other way to do it, unless of course i instantiate every object in every function again.

    Well, again thanks for your reply.

  6. #6
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hmm, i noticed another thing, now i have to put global $smarty in each function to be able and access it in the function.

    That doesn't make sense to me, is there no way to set a variable at the beginning so i can use that variable in all functions inside the page?

  7. #7
    SitePoint Wizard silver trophy someonewhois's Avatar
    Join Date
    Jan 2002
    Location
    Canada
    Posts
    6,364
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think you have to global every time, but it's not that hard, is it?

  8. #8
    SitePoint Guru
    Join Date
    Feb 2002
    Posts
    625
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    No it aint, but it's not really what i wanted, and it also does not make any sense to me...

  9. #9
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by datune
    Hmm, i noticed another thing, now i have to put global $smarty in each function to be able and access it in the function.

    That doesn't make sense to me, is there no way to set a variable at the beginning so i can use that variable in all functions inside the page?
    no you can't, or even if you can you shouldn't... the thing we're talking about here is scope... your object outside the function (which is $smarty in this case) would not be visible outside your function... that's why you need to declare it as global...

    but there's a quite fundamental error in setting a global variable in the function... now whenever you want to use the function (which is ShowCompanyDetails() in this case), you'd need to have a global $smarty variable which would then be the 'target' of the function... this makes for very rigid (inflexible) code which requires you to already have a variable $smarty so that you can call the function on it... i don't know how to put this better but someone more familiar with programming theory would be able to better explain this (that someone will come along... )

    if the function ShowCompanyDetails() is one which would work only on objects of the same class as $smarty, you could add it to the function definition... if you didn't write that class you could extend it with your own... the class method would then look like that:
    PHP Code:
    class Smarty
    {
      
    // snip

      
    function ShowCompanyDetails()
      {    
        
    $this->assign("whatever""Text bla bla");
      }

    }
    // end class definition

    $smarty->ShowCompanyDetails(); 
    if not, you should pass the object as an argument to the function instead so that it would work without requiring an already existing global variable:
    PHP Code:
    function ShowCompanyDetails($obj)
    {    
        
    $obj->assign("whatever""Text bla bla");
    }

    ShowCompanyDetails($smarty); 

  10. #10
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Here we go again...

    A while ago in this forum I said 'global variables are EVIL, and you should NEVER use them'. As you can imagine, that was quite a flamebait

    Funny thing was that I gave a lot of Very Good Reasons why global variables are so bad, while many people who claim it's okay to use them couldn't rightfully answer any of those reasons...

    So, even after that discussion, I still say that global variables are evil. Don't use them. And that's that. If you're interested in why you shouldn't use them, take a look at those other posts here at SitePoint.

    Vincent


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
  •