-2

here's the code!.Not working,Please help.

class user{
var idgen;

function uid(){
    //return uniqid (rand(), true);
    return "123";
    }

function gen() {
    $this->idgen=$this->uid();
            //$this->idgen=udi();//even this dint work
    }

function pint() {
    echo "The id is :".$this->idgen;
    }
}

$use= new user();
$use->gen();
$use->pint();
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Php Beginner
  • 61
  • 1
  • 1
  • 7

5 Answers5

3

public $idgen; instead of var idgen;

Tatu Ulmanen
  • 123,288
  • 34
  • 187
  • 185
1

Change:

var idgen;

With:

public $idgen = '';
Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
1

var is deprecated, and you forgot the $ in $idgen:

class user {
   private $idgen;

   // ... functions
}

You should activate error reporting on your webserver. Your original code will have generated an error; you just couldn't see it.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1

Change the second line of your code to

private $idgen;

and voila! :)

BTW it's worth setting error reporting on; it really helps debugging.

You can do it by editing the php.ini file or adding this somewhere in your project:

ini_set('error_reporting', E_ALL | E_STRICT);
ini_set('display_errors', 'On'); // <-- change this to off when live.

Put this in a file with other settings and include it in every page.

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
Ivan
  • 3,567
  • 17
  • 25
  • Thanks a ton.was trying to get firephp to work with firephp.Never got it working though.This'll cut short my debugging time 4 sure.Thanks a ton!!.. – Php Beginner Jul 20 '11 at 13:15
0

First make a __constructor function, and add a "$" to the var definition.

class user{

public $idgen;

function __constructor(){
    $this->gen();
}

function uid(){
    //return uniqid (rand(), true);
    return "123";
}

function gen() {
    $this->idgen=$this->uid();
    //$this->idgen=udi();//even this dint work
}

function pint() {
    echo "The id is :".$this->idgen;
}
}

$use= new user();
$use->pint();

Give your defined variables a default value... never assume that they will be given a value through the processing of class.

norris-md
  • 306
  • 2
  • 10