简体   繁体   中英

Variable within function accessible to outside world

I'd like to have access to $lastID when calling raw Is there a way to do this?

public static $lastID;  
public function raw($sql){

    if(!$result = mysql_query($sql)){
        throw new Exception("Could not perform query: " .mysql_error());
    }

    self::$lastID = mysql_insert_id();
    return($result);

}

Edit: it is a class member, it is static.

The context isn't clear as but it looks as though $lastID is a class memeber, in which case to access it from within methods of that class you should use:

$this->lastID;

The other issue I can see in your code is that this line will not work

self::$lastID = mysql_insert_id();

As $lastID isn't static. Either declare $lastID as static (in which case its state will be shared across all objects of the class)

//change this
var $lastID; 

//to this to declare $lastID as static
static $lastID;

Or use $this-> rather than self::

$this->lastID = mysql_insert_id();

you have to use inside raw

global $lastId;

Here's the related documentation http://ca.php.net/manual/en/language.variables.scope.php

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM