SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Oct 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    OOP question - global variables?

    Code:
    public class someting {
    
    private int num = 456;
    
    public void doSomething(){
    
    int variable = num + 456;
    }
    
    }

    In java, variables on the top can simply be accessed by referring to them, but if I want to access the same variable in PHP, I would have to do this:

    Code:
    $this->num

    is there a way to declare a variable in a way that it could be accessed anywhere in the class without $this keyword.
    I also want it to be private to other classes.

  2. #2
    SitePoint Guru rageh's Avatar
    Join Date
    Apr 2006
    Location
    London, Formerly Somalia
    Posts
    612
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it is possible but only in procedural coding. Not in classes. To get access to any variable declared in a class, you need the $this keyword
    ------------------

  3. #3
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use plain variables within a Method...

    PHP Code:
     class Hello
    {
      public function 
    Run()
      {
        
    $a 1;
        
    // Use $a anywhere inside of Run()
      
    }

    You can use globals but that will only allow you to access that variable within the Method it's declared I think, not the whole class. I could be wrong.

    There are Static variables that can be useful as well, but that is probably not what you're looking for

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Nope.

  5. #5
    SitePoint Evangelist
    Join Date
    Aug 2005
    Location
    Winnipeg
    Posts
    498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If the variable is a data member of a class you must some kind of scope resolution operator, whether that be $this->someVariable or A_Static_Member::someVariable

    Otherwise how would you differentiate from a local variable, or class variable?

    Cheers,
    Alex
    The only constant in software is change itself

  6. #6
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by PCSpectra View Post
    Otherwise how would you differentiate from a local variable, or class variable?
    Java lets you do it, and I kinda prefer it over the verbose $this-> syntax to be honest. But it's considered bad practice to make a local variable with the same name as an instance variable. The local variable just takes over for the duration of the scope block it was declared in, and using this.foo is how you access the instance variable instead of the local in that situation.

    Main thing I don't like about it is you still end up either using this, or picking a different name for setters and the like.
    Code:
    public void setAge(int years) {
        this.years = years;
    }
    or
    Code:
    public void setAge(int numYears) {
        years = numYears;
    }

  7. #7
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Even then though, I like the Java way much better.

  8. #8
    SitePoint Addict eanimator's Avatar
    Join Date
    Sep 2005
    Posts
    396
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use registry pattern Hope this helps

  9. #9
    Theoretical Physics Student bronze trophy Jake Arkinstall's Avatar
    Join Date
    May 2006
    Location
    Lancaster University, UK
    Posts
    7,062
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    The registry pattern won't help the OP here

    I have always prefered the 'Java' way of doing it (though it should be noted that Java wasn't the first or even most popular language that adopted this). However, PHP needs to maintain differences to other languages, and this is one of the ways.

    Some benefits can be found. For example, in Java I camelcase variables with the first letter also being uppercase, for example 'SomeVariableName'. For function parameters I need to use a lowercase first letter to distinguish between them, for example:
    Code java:
    public void setName(String name){
        Name = name;
    }
    With PHP I can keep to the same convention no matter what the origin of the variable:
    PHP Code:
    public function setName($Name){
        
    $this->Name $Name;

    Not exactly a massive benefit, but I suppose every little counts
    Jake Arkinstall
    "Sometimes you don't need to reinvent the wheel;
    Sometimes its enough to make that wheel more rounded"-Molona


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
  •