1

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');
    }
}
1
  • The linked article is about passing by reference, is that what you want/need? Commented Apr 30, 2011 at 21:48

4 Answers 4

5

Simply put the variable names in the constructor.

Take a look at the snippet below:

public function __construct( $aa )
{
   // some content here
}
Sign up to request clarification or add additional context in comments.

1 Comment

Note, though, that the variable in the function has no connection to the variable outside of it. It's common, but not necessary, to name it the same. But changing $aa in the function won't change the value of the real $aa, unless $aa is an object (as of PHP 5) or you pass it by reference.
3

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);

Comments

2
$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');

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.