SitePoint Sponsor

User Tag List

Results 1 to 10 of 10

Hybrid View

  1. #1
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Class vars - public or private?

    Hi:

    I don't often delve into OO but it's becoming more common in the PHP projects I develop.

    I just wanted to know simply - what are public and private variables within a class? Can you give me some basic examples?

    Also while we're on the subject - what are the differences between and circumstances of use of constructor and other types of class-methods?

    Thanks a lot

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Public variables are accessible from outside the class, which is frowned upon in the world of object oriented design. Private variables are only accessible to instances of the same class.

    The constructor is a special method that is automatically called when you create an instance of a class. It is used for initialising the instance. True OO languages also supply a corresponding method called a destructor, which is called automatically when an instance is destroyed.
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo
    Public variables are accessible from outside the class, which is frowned upon in the world of object oriented design. Private variables are only accessible to instances of the same class.
    Why is it frowned upon and are you able to give me a simple example of an instance of a class whose variables are accessible from outside of it?

    Quote Originally Posted by AutisticCuckoo
    The constructor is a special method that is automatically called when you create an instance of a class. It is used for initialising the instance. True OO languages also supply a corresponding method called a destructor, which is called automatically when an instance is destroyed.
    How exactly would you construct it and what does doing so actually do? Again an example or two would be very helpful

    Thanks a lot.

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Public data members break one of the key OO concepts, namely 'encapsulation'. Encapsulation means that an 'instance' (i.e. an object) of a class controls its internal data, and only allows access to it through member functions. You can have a getFoo() function that returns some data (by value, or as a copy), and a setFoo() function that allows others to modify data in a controlled way. For instance, if the data is a numeric value that must not be negative, the setFoo() member function could reject any value below zero.

    Example class:
    PHP Code:
    class Foo
    {
      public  
    $a;
      private 
    $b;
    }

    var 
    $myFoo = new Foo;

    $myFoo->13// Allowed.
    $myFoo->69// Not allowed! $myFoo->b is private. 
    The constructor is called when you create an instance of a class. Its name is the same as the class name. Example:
    PHP Code:
    class Bar
    {
      public 
    $x;

      function 
    Bar()
      {
        
    $this->1;
      }
    }

    var 
    $myBar = new Bar;
    echo 
    $myBar->x// Writes '1'. 
    Note that you don't explicitly call $myBar->Bar(). It's called automatically when you do 'new Bar'.
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Addict phptek's Avatar
    Join Date
    Jun 2002
    Location
    Wellington, NZ
    Posts
    363
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you - that was all very helpful!

  6. #6
    SitePoint Wizard DougBTX's Avatar
    Join Date
    Nov 2001
    Location
    Bath, UK
    Posts
    2,498
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by AutisticCuckoo
    For instance, if the data is a numeric value that must not be negative, the setFoo() member function could reject any value below zero.
    In PHP5 you get another option.

    Say you have a read/write property of a class, and you don't want to bother with any checking of what the property is set to. Instead of using a get/set pair, you can just use a public variable. But what if you want to add some sort of responce to the variable being set later?

    Well, you can make the variable protected, and then use PHP's __get($val) and __set($key, $val) magic functions to handle the request. This way, for simple public variables, you have a way to "upgrade" to a get/set system, but let yourself code more quickly using publics when you don't need to do anything complicated.

    hth,
    Douglas
    Hello World

  7. #7
    Non-Member
    Join Date
    Oct 2004
    Location
    downtown
    Posts
    145
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What I do is to keep a class property private, and all methods private as well, unless for certain a method has to be accessed outside the class.

    If you extend a class from a parent class you may note you are unable to use private so you use protected instead, as I see it anyways

  8. #8
    Employed Again Viflux's Avatar
    Join Date
    May 2003
    Location
    London, On.
    Posts
    1,127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also note that if you are using the PHP5 public/private/protected keywords, you also have at your disposal the __construct() function. You can use this as your constructor instead of a function that uses the same name as the class.

  9. #9
    simple tester McGruff's Avatar
    Join Date
    Sep 2003
    Location
    Glasgow
    Posts
    1,690
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

  10. #10
    Resident Java Hater
    Join Date
    Jul 2004
    Location
    Gerodieville Central, UK
    Posts
    446
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I quite often use protected myself....

    I know some people might look down at that as it's encorages the use of inheritance. However I quite often seem to end up generating an interface, then making a abstract base class to implment basic functionallity, and then entend once from there. I do normally try to avoid over entending where possible, however there are some cases where extending a bit is a better solution. Personally, I don't have too much issue with extending, provided the child class is not completely replacing what is defined in the base class. Likewise, there is no point in extending if the child class isn't using methods in the base class as this is also bloat.

    I often avoid using public where possible. I find in most cases getters and setters are much better as they allow you to impose restrictions, and do validation etc, which i like to do as it is defensive coding (and thus saves a fair bit of debugging in the long term)


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
  •