yuli chika
yuli chika

Reputation: 9221

How to pass php variable into a class function?

I want to pass php variable $aa into a class function. I have read some articles in php.net, but I still don't understand well. Can anyone help me put the variable into this class? thanks.

$aa='some word';
class Action {
    private $_objXML;
    private $_arrMessages = array();
    public function __construct() {        
        $this->_objXML = simplexml_load_file($aa.'.xml');
    }
}

Upvotes: 1

Views: 9179

Answers (4)

jefflunt
jefflunt

Reputation: 33954

Simply put the variable names in the constructor.

Take a look at the snippet below:

public function __construct( $aa )
{
   // some content here
}

Upvotes: 5

John Chadwick
John Chadwick

Reputation: 3213

I'm not sure what you mean... do you mean you want to access $aa in a function? If so:

$aa='some word';
class Action {
    private $_objXML;
    private $_arrMessages = array();
    public function __construct() {
        global $aa;
        $this->_objXML = simplexml_load_file($aa.'.xml');
    }
}

Or, on a per instance basis, you can do things like:

$aa='some word';
class Action {
    private $_objXML;
    private $_arrMessages = array();
    public function __construct($aa) {
        $this->_objXML = simplexml_load_file($aa.'.xml');
    }
}
new Action($aa);

Upvotes: 3

0x77D
0x77D

Reputation: 1574

I don't know php, but my logic and google say this:

class Action {
    private $_objXML;
    private $_arrMessages = array();
    public function __construct($aa) {        
        $this->_objXML = simplexml_load_file($aa.'.xml');
   }
}

$object = new Action('some word');

This is simply called pass a variable as parameter of a function, in this case the function is the constructor of Action

Upvotes: 1

Martijnc
Martijnc

Reputation: 61

$aa='some word';
class Action {
    private $_objXML;
    private $_arrMessages = array();
    public function __construct($aa) {        
        $this->_objXML = simplexml_load_file($aa.'.xml');
    }
}

And use it like this:

$instance = new Action('something');

Upvotes: 2

Related Questions