SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    basic OOP question

    Well I have decided to work in oop with my php scripts. I ran into my first problem within minutes. This was just a test case to transfer my line of thought over and it doesn't 'quite' work.

    PHP Code:
    <?php
        
    class Rectangle
        
    {
            var 
    $width;
            var 
    $height;
            function 
    Rectange($width,$height)
            {
                
    $this->width=$width;
                
    $this->height=$height;
            }
            function 
    area()
            {
                return (
    $this->width $this->height);
            }
            function 
    perimeter()
            {
                return (
    $this->width+$this->height)*2;
            }
        }
        
    $rec=New Rectangle(50,20);  //by itself doesn't work with references    
        
    $rec->width=50;   //can assign variables like this and it works
        
    $rec->height=10;
        
    $a=$rec->area();
        
    $b=$rec->perimeter();
        echo 
    $a
        ?>
    As you can see it just calculates the basic area and perimeter of a square. I know the code works but I can do it by simply passing the variables with the constructor I have to send the variables on assignment.

    Edit:


    Just in case this helps it returns 0 no matter what method I call when using the constructor alone.

    Is my code messed or is it something in php.ini. Thanks guys
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  2. #2
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: basic OOP question

    yes you can do $rec->width=some value outside the class cause PHP is not a full OOP... it doesn't hide object variables (no such thing as 'private' as in C)...

    and you can't return anything from the constructors... a workaround would be to have the constructor change an object variable like $this->error=1; where you can set the error code or watever...

    btw i hope this answers some of your qns cos i'm not really sure what you're asking

  3. #3
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: Re: basic OOP question

    Originally posted by redemption
    yes you can do $rec->width=some value outside the class cause PHP is not a full OOP... it doesn't hide object variables (no such thing as 'private' as in C)...

    and you can't return anything from the constructors... a workaround would be to have the constructor change an object variable like $this->error=1; where you can set the error code or watever...

    btw i hope this answers some of your qns cos i'm not really sure what you're asking
    Hmmm..ok let me explain a little clearer. when defined within the actual object constructor

    $obj=new Object(var1,var2)

    the variables don't seem to get passed. Which leaves me the options of assigning them after the constructor like above. I 'thought' it was suppost to work by referencing them within the constructor but it doesn't.

    This is an example taken out of Kevins new article. I just have some extra code at the bottom to test why it wasn't working.

    So this is the original code and should work (according to docs and Kevin)
    PHP Code:
    <?php
        
    class Rectangle
        
    {
            var 
    $width;
            var 
    $height;
            function 
    Rectange($width,$height)
            {
                
    $this->width=$width;
                
    $this->height=$height;
            }
            function 
    area()
            {
                return (
    $this->width $this->height);
            }
            function 
    perimeter()
            {
                return (
    $this->width+$this->height)*2;
            }
        }
        
    $rec=New Rectangle(50,20);
         echo 
    $rec->area();
            
    ?>
    Last edited by Maelstrom; Feb 3, 2002 at 01:00.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.

  4. #4
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    There is something very important missing from your class Rectangle. The constructor! This is Kevin's code from http://www.webmasterbase.com/article/662/24
    PHP Code:
    <?php
    class Rectangle{
      var 
    $width;
      var 
    $height;

      function 
    Rectangle($width$height)  {
        
    $this->width $width;
        
    $this->height $height;
      }

      function 
    area()  {
        return 
    $this->width $this->height;
      }

      function 
    perimeter()  {
        return (
    $this->width $this->height) * 2;
      }
    }
    ?>
    Note the class contains a function which shares the class name:
    function Rectangle($width, $height)

    This is the class constructor. A class constructor is a special function which is called automatically when an object of that class is instantiated. See http://www.php.net/manual/en/languag...onstructor.php

    Last edited by freakysid; Feb 3, 2002 at 02:30.

  5. #5
    Action! filmfoto's Avatar
    Join Date
    Dec 2001
    Location
    Sweden
    Posts
    278
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Re: basic OOP question

    Originally posted by Maelstrom
    PHP Code:
    function Rectange($width,$height)... 
    It's probably just a "typo", should be:
    PHP Code:
    function Rectangle($width,$height)... 




    Cheers.

  6. #6
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i'm sorry to say i didn't notice the typo...

    issit a typo Maelstrom? i'm sure he knows that constructors have the same name as the class...

    PS. i finally understand what you're asking...

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ha - yes, just a typo. Weird that I didn't notice that it was just a typo. I must have been thinking like a compiler - all I saw was that there was "technically" no constructor. Weird. I need more sleep

  8. #8
    What? Maelstrom's Avatar
    Join Date
    Oct 2001
    Location
    Whistler BC originally from Guelph Ontario
    Posts
    2,175
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes it was a typo. Such a simple error. Well at least now I am excited again

    Thanx for the help guys.
    Maelstrom Personal - Apparition Visions
    Development - PhP || Mysql || Zend || Devshed
    Unix - FreeBSD || FreeBsdForums || Man Pages
    They made me a sitepoint Mentor - Feel free to PM me or Email me and I will see if I can help.


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
  •