i am not getting o/p of this code, plz help

Discussion in 'PHP' started by vmathur03, Dec 4, 2011.

  1. #1
    Hi all,
    i am new in php and i am not getting o/p of this below code, plz help, tell me where i am doing wrong
    <?php

    class Person
    {
    var $name;

    function set_name($data)
    {
    $this->name;

    }
    function get_name()
    {
    return $this->name;
    }
    }
    $n= new Person;
    $n->get_name("vishal");
    ?>


    i am getting neither any warning nor any o/p..
    plz someone tell me where i am wrong
     
    vmathur03, Dec 4, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    1) get_name doesn't take an argument.

    2) You're not setting the name in set_name.
     
    Rukbat, Dec 4, 2011 IP
  3. vmathur03

    vmathur03 Active Member

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #3
    Thanks rukbat
    but even i have tried with below code, but i have not found any result

    class Person
    {
    var $name;

    function set_name($data)
    {
    $this->name;

    }
    function get_name()
    {
    return $this->name;
    }
    }
    $n= new Person;
    $n-> set_name("vishal");
    echo $n-> get_name();

    plz give some suggestions
     
    vmathur03, Dec 4, 2011 IP
  4. Rising_Star

    Rising_Star Active Member

    Messages:
    39
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    63
    #4
    <?php

    class person
    {
    var $name;

    function set_name($data)
    {
    $this->name=$data;
    }

    function get_name()
    {
    return $this->name;
    }
    }
    $n= new person;
    $n->set_name("Vishal");
    echo $n->get_name();
     
    Rising_Star, Dec 5, 2011 IP
  5. vmathur03

    vmathur03 Active Member

    Messages:
    24
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    86
    #5
    Thanks Rising _Star, now this code is running, thanks for help :)
     
    vmathur03, Dec 6, 2011 IP