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() {开发者_C百科
echo "The id is :".$this->idgen;
}
}
$use= new user();
$use->gen();
$use->pint();
public $idgen;
instead of var idgen;
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.
Change:
var idgen;
With:
public $idgen = '';
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.
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.
精彩评论