-1

I have a very simple class:

 class MyClass
 {
      public function someFunction()
      {
         echo array_key_exists( 'dynamicVariable', $GLOBALS ) ? 'true' : 'false';
      }
 } 

I want to define a variable 'on the fly' inside 'someFunction', but I can't seem to figure out how to do this inside function scope.

$classInstance = new MyClass();

$varName = 'dynamicVariable';
$classInstance->$varName;

What I want to do:

$classInstance = new MyClass();

$varName = 'dynamicVariable';
$classInstance->functionScopeReference->$varName;


$classInstance->myFunction(); <-- this will print TRUE

How can do the same thing, but define it in someFunction scope, instead of MyClass scope? Thanks

0x29a
  • 733
  • 1
  • 7
  • 23
  • 1
    there are ample "good" OOP tutorials; have you not read (some of) them? – Funk Forty Niner Nov 01 '16 at 12:18
  • 1
    Short answer, **you can't** from outside of the methods scope, unless the variable is a `property` used inside any method. – dbf Nov 01 '16 at 12:28
  • @dbf thank you! you can post this comment as an answer and I will mark it – 0x29a Nov 01 '16 at 12:30
  • after `$varName = 'dynamicVariable'; $classInstance->$varName = 1;` someFunction's `echo ( $this->dynamicVariable ) ? 'true' : 'false';` will print "true" after you call it with `$classInstance->myFunction();`. Forget the 101 course on object oriented programming, this will not be there – Federkun Nov 01 '16 at 12:32
  • 3
    There, I posted a (community wiki) answer. And there's no need to get all hot under the collar here. I believe I was polite and wasn't obnoxious in any way, so there's no need for being snarky. – Funk Forty Niner Nov 01 '16 at 13:12

2 Answers2

4

Posting as a community wiki in order to satisfy the OP, since dbf hasn't posted the comment to close the question.

"Short answer, you can't from outside of the methods scope, unless the variable is a property used inside any method. – dbf"

...

"@dbf thank you! you can post this comment as an answer and I will mark it – 0x29a"

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • to me "unclear what you're asking" is a more suitable way to close this question, but whatever. +1, for OP it's impossibile, so we can't help. – Federkun Nov 01 '16 at 13:25
  • @Federkun I voted as "too broad". Yet, given the additional edits and their not marking the (community wiki) answer, is a good vote as "unclear". Btw, I make no rep on these, in case you may not know ;-) but thanks for the vote anyway, *cheers* – Funk Forty Niner Nov 01 '16 at 13:26
  • @Federkun "unclear what you're asking"? I clearly provided a code example of what I have done and **what I'm trying to do** + additional explanation and dbf understood me clearly. I apologize if my language is not clear, but I did explain and give example on what I'm trying to do. I appreciate Fred -ii- efforts, thank you, I think this is the correct answer and will help out anyone who's trying to do the same weird thing I was trying. – 0x29a Nov 01 '16 at 16:32
1

By using the $this keyword... I would suggest a good 101 course on object oriented programming which should explain scope better then I could...

    class MyClass
{
    public function someFunction($foo)
    {
        $this->dynamicVariable = $foo;
    }
}

$classInstance = new MyClass();
$classInstance->someFunction('dynamicVariable');

echo $classInstance->dynamicVariable;

EDIT: to better answer OP's question (sorry for not reading that correctly!): although it does not change scope, a workaround would be to use getters and setters and make your properties private:

class MyClass
{
    private $property_one; // can't access this without using the getPropertyOne function
    private $property_two;

    public function setPropertyOne($bool)
    {
        $this->property_one = $bool;
    }

    public function getPropertyOne()
    {
        return $this->property_one;
    }

    public function setPropertyTwo($bool)
    {
        $this->property_two = $bool;
    }

    public function getPropertyTwo()
    {
        return $this->property_two;
    }
}

$classInstance = new MyClass();

// trying to access the properties without using the functions results in an error
echo $classInstance->property_one;

$classInstance->setPropertyOne(true);
echo $classInstance->getPropertyOne();

$classInstance->setPropertyTwo(false);
echo $classInstance->getPropertyTwo();
Community
  • 1
  • 1
Jethro Hazelhurst
  • 3,230
  • 7
  • 38
  • 80
  • 1
    *"I would suggest a 101 course on object oriented programming."* - That alone deserves an upvote ;-) – Funk Forty Niner Nov 01 '16 at 12:14
  • Did you even read the question? You're answer is wrong, even from the point you understood my question. I suggest you to take curse in 101 logic – 0x29a Nov 01 '16 at 12:17
  • Do you understand that you didn't answer my question in any way? you just gave me a basic OOP 'workaround' that is completely unrelated to my 'Defining DYNAMIC variable' goal and told me I need to learn the basics of OOP. The answer to this question as @dbf pointed out is that "It's not possible from outside the class". No need to attack the intelligence of others if you misunderstood the question and gave me an answer to a completely different question I never asked in the first place. I hate when people don't fully understand the problem and think the first somewhat related solution "is it!" – 0x29a Nov 01 '16 at 12:59
  • 1
    Stackoverflow allows you to answer your own question and then accept that answer after some time (I think two days?). So if you have the answer go ahead an share it for the benefit of the community. Also feel free to down vote any unhelpful answers. – Jethro Hazelhurst Nov 01 '16 at 13:03