SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Addict sorin21us's Avatar
    Join Date
    Mar 2009
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    basic question about oop

    I started with oop but I'm confused. I have this class:
    PHP Code:
    class Box
    {
      var 
    $contents;

      function 
    Box($contents) {
        
    $this->contents $contents;
      }

      function 
    get_whats_inside() {
        return 
    $this->contents;
      }

    In this class, please someone tell me, what is contents from my function Box? I mean the one after the this word. And I still don't get how to imagine the this word when I use the object.

  2. #2
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This class is setup for php4, more than now you're running php 5. The class should be re formated to look like this:
    PHP Code:
    class Box{
         private 
    $contents;
         public function 
    __construct($contents){
              
    $this->contents $contents;
         }

         public function 
    whats_inside(){
              return 
    $this->contents;
         }


    So in php4 the constructor was a function that has the same name as teh class, in php5 they've created a constructor function called __construct. A constructor is a function that is called whenever a class is instaniated, everytime you do
    PHP Code:
    $red = new box(); 
    Now in this case we've decided to pass in variable into the box::contents. Normally class attribtues can be accessed via $box->contents, but since we've marked contents as a private attribute you have use "setter and getters" to modify the variable. In this case the constructor is the setter. So say you had
    PHP Code:
    $variable "Jack";
    $red = new Box($variable); 
    This creates the box object using hte __constructor, and passes in the variable "Jack" into function and sets the private attribtue contents to "Jack". Try the following code, it might help you figure it out.

    PHP Code:
    $red = new Box("Jack");
    var_dump($red);

    class 
    new_box{
         private 
    $contents;
         public function 
    __construct($contents){
              
    $this->contents $contents;
              
    debug_print_backtrace();
         }

         public function 
    whats_inside(){
              return 
    $this->contents;
         }
    }

    $pink new_box("Stuffed Dog"); 
    That will show you the path the code took to create your box object.

  3. #3
    SitePoint Enthusiast nrg_alpha's Avatar
    Join Date
    Dec 2008
    Posts
    81
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    To add to bailey's explanation, the private declaration of $contents in that example basically restricts access to that property to be accessable only within that class.. setting accessablility using public, private or protected is called visibility.

    To illustrate 'setters' and 'getters' (also known as accessor methods) that bailey was talking about, here is a version of the previous example that uses those instead of using the constructor as a setter:

    Example:
    PHP Code:
    class new_box{
        private 
    $contents;

        public function 
    setContents($contents) {
           
    $this->contents $contents;
        }

        public function 
    getContents() {
           return 
    $this->contents;
        }
    }

    $box01 = new new_box;
    $box01->setContents('I am contents of box01!');
    echo 
    $box01->getContents(); // Output: I am contents of box01! 
    Off Topic:


    Typically, class naming convention starts the class name off with a captial letter, but this is not a hard rule set in stone.
    In any case, you read up about PHP 5 classes here.

  4. #4
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class Box
    {

        
    // contents is a variable to pass around the "Box" class,
        // similar to a variable inside of function.
        // But a variable inside a class can be passed inside "methods", or the functions of the class.
        
    var $contents;

        
    // The method name "Box" is confusing since its the same as the class name.
        // the function name has nothing to do with the class name really.  You are
        // just passing a variable through it.
        
    function Box($contents
        {

        
    // $this means "This class", so "The Box Class'es" variable called "contents", equals
        // whatever you pass through the Box method.
            
    $this->contents $contents;

        }


        
    // This is self explanatory.
        
    function get_whats_inside() 
        {

            return 
    $this->contents;

        }



    $test = new Box;
    $test->Box('hello');
    echo 
    $test->get_whats_inside();
    // Will return 'hello'; 
    And easier way to do this way, you'll see why at the bottom when I make an object:

    PHP Code:
    class Box
    {
        protected 
    $contents;
        
        function 
    __construct($var)
        {
            
    $this->contents $var;
        }
        
        public function 
    get_whats_inside()
        {
            return 
    $this->contents;
        }
    }

    $test2 = new Box('hey cool, saved a line');
    echo 
    $test2->get_whats_inside();
    // Will return 'hey cool, saved a line' 


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
  •