-2
<?php
class roxy
{

   function sam()
   {
       $name;
       $class;
       $age;
        echo $name;
        echo $class;
        echo $age;
   }

}
$kush= new roxy();
$kush->name="sachin";
$kush->class=12;
$kush->age=21;
$kush->sam();

?>

Here variables are declared localy and when value of variables set through object and after that i call the function it shows me undefined in the result screen.

rock001
  • 5
  • 3

4 Answers4

5

This is not how oop works: Here is a working example of what you are trying to achieve:

<?php
class roxy
{
   public $name;
   public $class;
   public $age;

   public function sam()
   {
       echo $this->name;
       echo $this->class;
       echo $this->age;
   }

}

$kush= new roxy();
$kush->name="sachin";
$kush->class=12;
$kush->age=21;
$kush->sam();

?>
Daan
  • 12,099
  • 6
  • 34
  • 51
1

That is because your function sam() is not referring to the variables you have declared before as variables of your object. Instead it echos the variables you have just defined within your function sam(), which are - of course - undefined.

This should work:

class Roxy {
   public $name;
   public $xclass;
   public $age;

   public function sam() {
        echo $this -> name;
        echo $this -> xclass;
        echo $this -> age;
   }

}

$kush= new Roxy();

$kush->name="sachin";
$kush->xclass=12;
$kush->age=21;

$kush->sam(); // > sachin > 12 > 21

+ class is a reserved keyword in php. You shouldn't use it. Instead use xclass for example.

Robin
  • 1,208
  • 8
  • 17
  • `class` isn't a reserved namespace, it's a reserved keyword. Using it as a property is not the best of ideas, fair enough, but it won't cause problems if you do – Elias Van Ootegem May 08 '14 at 12:22
  • Thanks for the hint about `class` being a reserved keyword instead of a reserved namespace. Still I think it's not a good idea to use. – Robin May 08 '14 at 12:28
  • @EliasVanOotegem Although "class" is a reserved keyword, it can still be used. This I tested with PHP 5.4. Yet as you say, it's not the best of ideas, I agree. – Funk Forty Niner May 08 '14 at 12:46
  • @Fred-ii-: That's exactly what I said: _"it WON'T cause problems if you do"_. But that's irrelevant, since we all agree that doing so is a bad idea – Elias Van Ootegem May 08 '14 at 12:59
  • @EliasVanOotegem Indeed. I shy away from using any type of reserved word, be it using classes, SQL, PHP etc. It's always best not to. Cheers – Funk Forty Niner May 08 '14 at 13:01
0

You need to use $this to refer to a class' own attributes. To get the behaviour you want change your class to look like this:

class roxy
{
    public $name;
    public $class;
    public $age;
    public function sam()
    {
        echo $this->name;
        echo $this->class;
        echo $this->age;
    }
}
nettux
  • 5,270
  • 2
  • 23
  • 33
0

1- your variables are defined inside the function and not the class!

2- they should be declared as public (as well as the function)

3- you should use $this to call them inside the function

laurent
  • 418
  • 3
  • 7