PHP Class starting Classes v. WTF IS GET/SET?

Discussion in 'OT Technology' started by brds, Feb 18, 2008.

  1. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,642
    Likes Received:
    13
    Location:
    Atlanta
    I just started doing my PHP homework today and ran into a little snag. We just started doing classes (which i've never done before) and i need to know how to configure the get/set for my class. Below are the instructions and what i've got so far:

    Instructions:
    What I've Got So Far:
    Code:
    <?php
        class WebPage
        {
            private $title;
            private $heading;
            private $content;
            
            function WebPage($funcTitle, $funcHeading, $funcContent)
            {
                $title = $funcTitle;
                $heading = $funcHeading;
                $content = $funcContent;
            }
            
            
        }
    ?>
    
    I'm stuck on #4. I've tried looking around google, but didn't find anything that helped. If anybody can shed some light onto the situation, it'd be greatly appreciated. :wiggle:
     
  2. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    get methods retrieve information. set methods store information.

    if you have a Person class with the variables Age, Height, and Name, the get methods would be:

    getAge() { return age; }
    getHeight() { return height; }
    getName() {return name; }

    the set methods would store variables:

    setAge(int a) {age = a;}
    setHeight(float h) {height = h;}
    setName(String n) {name = n;}
     
  3. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,642
    Likes Received:
    13
    Location:
    Atlanta
    I put in the following code:

    Code:
    <?php
        class WebPage // Create Class
        {
            /* Set Private Variables */
            private $title;
            private $heading;
            private $content;
            
            /* Constructor */
            function WebPage($funcTitle, $funcHeading, $funcContent)
            {
                $title = $funcTitle;
                $heading = $funcHeading;
                $content = $funcContent;
            }
            
            /* Get Methods */
            getTitle() { return title; }
            getHeading() { return heading; }
            getContent() { return content; }
            
            /* Set Methods */
            setTitle(String t) { title = t; }
            setHeading(String h) { height = h; }
            setContent(String c) { content = c; }
        }
    ?>
    
    I get syntax errors on all the get's and the return's for each get. Did i do the get/set right? :dunno:
     
  4. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    They are still functions, you'd have it like:

    Code:
            /* Get Methods */
            function getTitle() { return title; }
            function getHeading() { return heading; }
            function getContent() { return content; }
            
            /* Set Methods */
            function setTitle(String t) { title = t; }
            function setHeading(String h) { height = h; }
            function setContent(String c) { content = c; }
     
  5. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    they are functions, but you also forgot the "$" to denote it's a variable.

    Code:
    <?php
        class WebPage // Create Class
        {
            /* Set Private Variables */
            private $title;
            private $heading;
            private $content;
            
            /* Constructor */
            function WebPage($funcTitle, $funcHeading, $funcContent)
            {
                $title = $funcTitle;
                $heading = $funcHeading;
                $content = $funcContent;
            }
            
            /* Get Methods */
            function getTitle() { return $title; }
            function getHeading() { return $heading; }
            function getContent() { return $content; }
            
            /* Set Methods */
            function setTitle(String t) { $title = t; }
            function setHeading(String h) { $height = h; }
            function setContent(String c) { $content = c; }
        }
    ?>
    
     
  6. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Um, using $ where applicable too :hs:.
     
  7. HardTech

    HardTech hungry

    Joined:
    May 5, 2000
    Messages:
    28,103
    Likes Received:
    1
    Location:
    NorCal
    no.. I did my syntax in Java. You'll have to port over to PHP
     
  8. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,642
    Likes Received:
    13
    Location:
    Atlanta
    Code:
    <?php
        class WebPage // Create Class
        {
            /* Set Private Variables */
            private $title;
            private $heading;
            private $content;
            
            /* Constructor */
            function WebPage($funcTitle, $funcHeading, $funcContent)
            {
                $title = $funcTitle;
                $heading = $funcHeading;
                $content = $funcContent;
            }
            
            /* Get Methods */
            function getTitle() { return $title; }
            function getHeading() { return $heading; }
            function getContent() { return $content; }
            
            /* Set Methods */
            function setTitle($t) { $title = $t; }
            function setHeading($h) { $height = $h; }
            function setContent($c) { $content = $c; }
        }
    ?>
    
    Does that look about right to everyone?

    (P.S. - Thanks for the help guys :wavey:)
     
  9. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Looks good, make sure you change height to heading :)

    Code:
    function setHeading($h) { $heading = $h; }
    
     
  10. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,642
    Likes Received:
    13
    Location:
    Atlanta
    good call
     
  11. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    I don't use classes often but the 'proper' method of setting and getting variables might be by using this.

    Code:
    function setHeading($h) { $this->heading = $h; }
    Also, in the WebPage function you might want to use the Set Methods to set the variables.

    Code:
    $this->setTitle($funcTitle);
    $this->setHeading($funcHeading);
    $this->setContent($funcContent);
    
     
  12. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    both true.
     
  13. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,642
    Likes Received:
    13
    Location:
    Atlanta
    Alright, I know i'm doing something wrong... just dunno what... here's the PHP error i'm getting when i try to view the page.

    Here's my class file:
    Code:
    <?php
        class WebPage // Create Class
        {
            /* Set Private Variables */
            private $title;
            private $heading;
            private $content;
            
            /* Constructor */
            function WebPage($funcTitle, $funcHeading, $funcContent)
            {
                $this->setTitle($funcTitle);
                $this->setHeading($funcHeading);
                $this->setContent($funcContent);
            }
            
            /* Get Methods */
            function getTitle() { return $title; }
            function getHeading() { return $heading; }
            function getContent() { return $content; }
            
            /* Set Methods */
            function setTitle($t) { $title = $t; }
            function setHeading($h) { $heading = $h; }
            function setContent($c) { $content = $c; }
            
            function displayInfo($title, $heading, $content)
            {
                echo "
                    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
                    <html xmlns='http://www.w3.org/1999/xhtml'>
                    <head>
                    <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
                    <title>$title</title>
                    </head>
                    
                    <body>
                    <h1>$heading</h1>
                    $content
                    </body>
                    </html>
                ";
            }
        }
    ?>
    
    Here's my controller file (index.php):
    Code:
    <?php 
        include("class.php");
        $pageInfo = new WebPage("Assignment 7", "OOP in PHP", "This is the seventh assignment of the semester and the first to deal with object oriented programming.");
        
        $pageInfo->setTitle($pageInfo->title);
        $pageInfo->getTitle();
        
        $pageInfo->displayInfo($pageInfo->title, $pageInfo->heading,  $pageInfo->content);
    ?>
    
    I know that the index.php isn't done, but I figured I should get the title working before I start looking at everything else. Anybody see where I went wrong?
     
  14. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    you have the variables' scope as "private" you can't access them outside the class.

    I have no fucking clue what you're trying to do in the second file.


    I can tell you know that programming is NOT the career for you.
     
  15. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    index.php:
    Code:
    <?php
    
    require_once("clsWebPage.php");
    
    $test = new WebPage("test title", "test heading", "test content");
    
    $test->displayInfo();
    
    ?>
    clsWebPage.php:
    Code:
    <?php
        class WebPage
        {
            private $strTitle;
            private $strHeading;
            private $strContent;
            
            function __construct($strTitle, $strHeading, $strContent)
            {
                $this->setTitle($strTitle);
                $this->setHeading($strHeading);
                $this->setContent($strContent);
            }
            
            /* Get Methods */
            function getTitle()
    	{
    		return $this->strTitle;
    	}
    
            function getHeading()
    	{
    		return $this->strHeading;
    	}
    
            function getContent()
    	{
    		return $this->strContent;
    	}
            
    
            function setTitle($strInput)
    	{
    		$this->strTitle = $strInput;
    	}
    
            function setHeading($strInput)
    	{
    		$this->strHeading = $strInput;
    	}
    
            function setContent($strInput)
    	{
    		$this->strContent = $strInput;
    	}
            
            function displayInfo()
            {
    		echo "<!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 
    
    'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>";
    		echo "<html xmlns='http://www.w3.org/1999/xhtml'>";
                    echo "<head>";
                    echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />";
                    echo "<title>".$this->getTitle()."</title>";
                    echo "</head>";
                    echo "<body>";
                    echo "<h1>".$this->getHeading()."</h1>";
                    echo $this->getContent();
                    echo "</body>";
                    echo "</html>";
    
            }
        }
    ?>



    [output]
    Code:
    <!DOCTYPE html PUBLIC '-//W3C//DTD XHTML 1.0 Transitional//EN' 'http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd'>
    <html xmlns='http://www.w3.org/1999/xhtml'>
    <head>
    <meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
    <title>test title</title>
    </head>
    <body>
    <h1>test heading</h1>
    test content
    </body>
    </html>
     
  16. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,642
    Likes Received:
    13
    Location:
    Atlanta
    Well excuse me for trying to learn something. I'm sure you didn't just sit down and know OOP. I haven't even gone over any of this in class yet, i was just trying to get a head start since i'm going out of town this wkend.

    Thanks for the help though. :wiggle:
     
  17. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    OOP was a very easy concept for me. And when I read about it and began understanding how it worked I just kept nodding my head... Because it was logical for me.

    Programming is a different mindset... And not everyone posesses it.

    By the way, OOP is super-basic stuff... It's a first-year class for most people, and some even take it pre-college. It only gets harder from here. I'm not trying to be mean, but most people change their major a few times, so just be sure that this is what you wanna do the rest of your life.
     
  18. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,642
    Likes Received:
    13
    Location:
    Atlanta
    I'm a senior in FSU who's taking PHP as an elective. :mamoru:
     
  19. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,642
    Likes Received:
    13
    Location:
    Atlanta
    P07r0457, i'm getting the following errors after using your code as an example:

    Code:
    [B]Warning[/B]: Missing argument 1 for WebPage::displayInfo(), called in /opt/iSpaceWeb/ama04e/html/spring2k8/php/assgn7/index.php on line 7 and defined in [B]/opt/iSpaceWeb/ama04e/html/spring2k8/php/assgn7/class.php[/B] on line [B]27[/B]
    
    [B]Warning[/B]: Missing argument 2 for WebPage::displayInfo(), called in /opt/iSpaceWeb/ama04e/html/spring2k8/php/assgn7/index.php on line 7 and defined in [B]/opt/iSpaceWeb/ama04e/html/spring2k8/php/assgn7/class.php[/B] on line [B]27[/B]
    
    [B]Warning[/B]: Missing argument 3 for WebPage::displayInfo(), called in /opt/iSpaceWeb/ama04e/html/spring2k8/php/assgn7/index.php on line 7 and defined in [B]/opt/iSpaceWeb/ama04e/html/spring2k8/php/assgn7/class.php[/B] on line [B]27[/B]
    
    EDIT: NM, i got it... thanks again
     
    Last edited: Feb 18, 2008
  20. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    is OOP REALLY that useful in PHP? Besides writing shit to a file? I figure AJAX OOP would be msot important.
     
  21. P07r0457

    P07r0457 New Member

    Joined:
    Sep 20, 2004
    Messages:
    28,491
    Likes Received:
    0
    Location:
    Southern Oregon
    Yes. OOP is an important concept in ANY application -- web-based or otherwise.
     
  22. whup

    whup I wish you had children and.. so that I could step

    Joined:
    Feb 12, 2007
    Messages:
    1,603
    Likes Received:
    0
    Yet you missed $this :mamoru:

    Well it makes things easier if you're trying to use OOP everywhere, especially as it's the accepted way of programming nowadays over functional. The advantages of OOP over functional make it worthwhile using in PHP over functional style.

    I hate ugly get and set methods. I wish PHP had C#-style properties. At least there's going to be namespaces in PHP6.
     
  23. ge0

    ge0 New Member

    Joined:
    Oct 31, 2005
    Messages:
    8,398
    Likes Received:
    0
    Location:
    JERSEY
    I realize OOP is real important in programming ( I am a Java, C++/C# programmer) but, are you able to pass objects from page to page in php? Or onc eyou exit the page, thats it, the object is gone?
     
  24. CyberBullets

    CyberBullets I reach to the sky, and call out your name. If I c

    Joined:
    Nov 13, 2001
    Messages:
    11,865
    Likes Received:
    0
    Location:
    BC, Canada/Stockholm, Sweden
    Save yourself a ton of time, and do this:

    Code:
    class test
    {
     public function _get( $variable )
     {
      if( isset( $this->$variable ) ) return $this->$variable;
      return null;
     }
    
     public function _set( $variable, $value )
     {
      $this->$variable = $value;
     }
    }
    
    no need to specify 1000 functions to do your get and sets. fyi i didn't compile it, but it should work fine
     

Share This Page