开发者

Calling functions from within each other not working

开发者 https://www.devze.com 2023-03-21 11:29 出处:网络
here\'s the code!.Not working,Please help. class user{ var idgen; function uid(){ //return uniqid (rand(), true);

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消