1

In my class, I am trying to set the value of a variable in a function other than __construct()

But I need that variable value in another function.

Here is what I have tried. But not works Properly.

I expect to print getty images but I get nothing :(

<?php

class MyClass{
    public $var;

    public function __construct(){
        $this->var;
    }

    public function setval(){
        $this->var = 'getty Images';
    }

    public function printval(){
        echo $this->var;
    }
}

$test = new MyClass();
$test->printval();
3
  • What do you expect? and what are you getting? Commented Oct 8, 2017 at 16:46
  • I expect to print getty images but i get nothing :( Commented Oct 8, 2017 at 16:47
  • 1
    Why do you expect to print something since you never put anything in $this->var? Commented Oct 8, 2017 at 17:03

3 Answers 3

5

Your constructor does nothing, you need to invoke the method for it to do something.

class MyClass{
    private $var;

    public function __construct() {
        // When the class is called, run the setVal() method
        $this->setval('getty Images');
    }

    public function setval($val) {
        $this->var = $val;
    }

    public function printval() {
        echo $this->var;
    }
}

$test = new MyClass();
$test->printval(); // Prints getty Images
Sign up to request clarification or add additional context in comments.

7 Comments

This is like, exactly the same as setting the value in the constructor, except that it's more indirected. Why make simple when you can confuse the hell out of everyone? ;)
This is not a setter. A setter would be function setValue($v) { $this->value = $v; }. And no, setter injection is not generally better than constructor injection. This is somewhat of a debate, but I literally never use it as it mostly leads only to problems down the road. In this specific case, there is literally no difference as the value is not passed to the setter function. This is dead code. So yeah, my point is that this function should be refactored in the constructor, as it is not actually used outside of the class but in the constructor.
I... don't know. The question is about setter and getter function. I would rather use the setter function perperly in the constructor (that is, pass 'getty Images' to the setter function $this->setVal('getty Images');, so that setter / getter stays in this answer). If you don't mind me doing it, I'd be happy to oblige.
I'm not saying you are wrong actually I'm here to learn more than to give, but OP said i am trying to set value of a variable in a function other than __construct() he insists on setting the value of the var in a method.
There, I believe I can't express myself, I edited it in a way that, I believe, keeps the setter/getter intent of your answer, answers the question, and actually implements a proper setter getter.
|
1

You need to call your setval() method to actually set a value.

Try:

<?php

$test = new MyClass();
$test->setval();
$test->printval();

If your are happy having a fixed value, setting the variable in the __construct() will work fine and i would recommend this approach.

If however you want a dynamic value you could tweak your setval method to acccept a parameter and save the passed parameter to your object for rendering as part of the printval() call.

Comments

0

you need first to set value to your variable before you print it

<?php

class MyClass{
    public $var;

    public function setval(){
        $this->var = 'getty Images';
    }

    public function printval(){
        echo $this->var;
    }
}

$test = new MyClass();
$test->setval();
$test->printval();
?>

output:

getty Images

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.