0

This is what I want to do:

class Contacts {

    private $_plural = 'contacts';
    private $_single = 'contact';
    private $_factory = 'contactfactory';
    private $_model = 'contact_model';

    private $_idname = $_plural . "Id";

    function a($$_idname = 0) {

    }
}

These two lines:

private $_idname = $_plural . "Id";

and

function a ($$_idname = 0) {

aren't working. Why? And how can I fix this?

EDIT

About the function argument:

If $_idname = "contactId" I want the argument to be $contactId. That's why I have two dollar signs there. This might be not the correct way to handle this, but this is what I want to accomplish.

1
  • Firstly remove the second dollar in the variable name: function a ($_idname = 0) Commented Mar 12, 2015 at 22:32

2 Answers 2

2

According to PHP's documentation, you must initialize a class attribute with a constant value:

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

A fix for this would be using a class constructor:

function __construct() {
    $this->_idname = $this->_plural . "Id";
}

Also, you can't use dynamic variable names on functions or methods:

Please note that variable variables cannot be used with PHP's Superglobal arrays within functions or class methods. The variable $this is also a special variable that cannot be referenced dynamically.

Sign up to request clarification or add additional context in comments.

1 Comment

I indeed thought about doing it the way in the constructor, but I thought it might be cleaner and clearer doing it there where I declare the class variables. Thanks for the information anyways.
1

You could change

private $_idname = $_plural . "Id";

to

private $_idname;
public function __construct(){
  $this->_idname = $this->_plural.'Id';
}

first.

Not seeing enough in function a. Probably more like:

public function a($really = 'What is the point of those underscores?'){
  ${$this->_idname} = $really; // local $contacts var holds $really
}

I'm really guessing that you want to have a method that will automatically change your instantiated Object property. You don't need a variable variable for that. If you want to affect a variable which you are passing as an argument it's &$yourVar. There is no need to pass a property of an instantiated Object to its own method, since you already have access to it within the method with $this->yourVar.

1 Comment

$this->_plural, doesn't work either. I get the error: Parse error: syntax error, unexpected '$this' (T_VARIABLE) about the function variable. I'll edit my question

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.