-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
  • 369,052
  • 73
  • 620
  • 1,021
Php Beginner
  • 61
  • 1
  • 1
  • 7

5 Answers5

3

public $idgen; instead of var idgen;

Tatu Ulmanen
  • 119,594
  • 33
  • 182
  • 182
1

Change:

var idgen;

With:

public $idgen = '';
Pablo Santa Cruz
  • 170,119
  • 31
  • 233
  • 283
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
  • 369,052
  • 73
  • 620
  • 1,021
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
  • 369,052
  • 73
  • 620
  • 1,021
Ivan
  • 3,479
  • 16
  • 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
  • 196
  • 1
  • 9