1

Can I assign to a property a value in the constructor, without define any parameter, using for instance an external function?

Example

function my_external_function() {

     return 'Good Morning World';

}

class MyClass {

   protected $_my_property;

   public function __construct() {

        $this->_my_property = my_external_function() != '' ? my_external_function() : 'Good night World!';

   }

   public function getOtherMethod() {

       return $this->_my_property;

   }

}

$obj = new MyClass();
echo $obj->getOtherMethod();
5
  • Why yes, yes you can... Commented May 23, 2016 at 21:12
  • 1
    Have you tried it? Though calling the function twice is highly inefficient Commented May 23, 2016 at 21:13
  • Why would you want to do this? Just curious. Commented May 23, 2016 at 21:17
  • 1
    You can do that, but beware - you will face the wrath of the next guy who maintains your code. Trust me on that one. And it won't be pretty. Commented May 23, 2016 at 21:20
  • Thank you for your answers and suggests. I was insecure for this approach and your replies has been instructive. Commented May 24, 2016 at 5:43

2 Answers 2

2

You can do this. The code in your question will work, but there is a problem with this approach.

If you write your class this way, it will always depend on that external function, but it will have no control over whether or not it even exists, let alone whether or not it will return a value the constructor can use. If you move, rename, or modify the external function, it could change the behavior of your class in unpredictable ways.

I would recommend something like this instead, which I think may accomplish what you're trying to accomplish (not sure) without forcing your class to blindly depend on an external function.

class MyClass {

    protected $_my_property = 'Good night World!';  // set a default value here

    public function __construct($x = null) {  // give your constructor an optional argument
        if ($x) {                             // use the optional argument if it's provided
            $this->_my_property = $x;
        }
    }

    public function getOtherMethod() {
        return $this->_my_property;
    }
}

You can still create an instance of your class with no argument

$obj = new MyClass();

and when you call $obj->getOtherMethod(); you'll get the default value.

You can still use the external function; just let it pass its value into your object's constructor instead of using it within the constructor.

$obj = new MyClass(my_external_function());
Sign up to request clarification or add additional context in comments.

1 Comment

thanks. Clarifying answer! Sorry for syntax error, was a oversight!
1

Yes, but you better avoid such tricky dependencies.

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.