1

I'm quite inexperienced with OOP PHP but here's my question...let's say I have this class with one property:

class myClass {

    public $property = array();

    public function getProperty() {
        return $this->property;
    }

}

How would it be possible to change the value of $property without altering the class itself in any way, or by instantiating an object out of it, then changing its property. Is there any other way of doing it? Using scope resolution?

Hope that makes sense, any help would be much appreciated.

Dannyd86
  • 87
  • 3
  • 14
  • 3
    Do you mean [static variables](http://php.net/manual/en/language.oop5.static.php)? – Waleed Khan Oct 06 '13 at 02:34
  • If you don't instantiate the class, there is no property, since there isn't an object. Are you wanting to change the class definition itself? This can't be done in php directly. You would need to extend the class and overwrite the property and call the extended class – Anthony Oct 06 '13 at 02:37
  • While you can do this, this is **a bad practise**, because of 2 things: It breaks **encapsulation**, and introduces **another form of global state** – Yang Oct 06 '13 at 05:16

2 Answers2

6

What you want is a static member

class MyClass {
   public static $MyStaticMember = 0;

   public function echoStaticMember() {
      echo MyClass::$MyStaticMember;
      //note you can use self instead of the class name when inside the class
      echo self::$MyStaticMember;
   }

   public function incrementStaticMember() {
      self::$MyStaticMember++;
   }
}

then you access it like

MyClass::$MyStaticMember = "Some value"; //Note you use the $ with the variable name

Now any instances and everything will see the same value for whatever the static member is set to so take for instance the following

function SomeMethodInAFarFarAwayScript() {
   echo MyClass::$MyStaticMember;
} 

...

MyClass::$MyStaticMember++; //$MyStaticMember now is: 1

$firstClassInstance = new MyClass();

echo MyClass::$MyStaticMember; //will echo: 1
$firstClassInstance->echoStaticMember(); //will echo: 1

$secondInstance = new MyClass();
$secondInstance->incrementStaticMember(); // $MyStaticMember will now be: 2

echo MyClass::$MyStaticMember; //will echo: 2
$firstClassInstance->echoStaticMember(); //will echo: 2
$secondInstance->echoStaticMember(); //will echo: 2

SomeMethodInAFarFarAwayScript(); //will echo: 2

PHPFiddle

Patrick Evans
  • 41,991
  • 6
  • 74
  • 87
2

I hope this is what you are looking for

<?php

class myClass {

    public $property = array();

    public function getProperty() {
        print_r($this->property);
    }

}


$a = new myClass();
$x = array(10,20);

$a->property=$x; //Setting the value of $x array to $property var on public class
$a->getProperty(); // Prints the array 10,20

EDIT :

As others said , yes you need the variable to be declared as static (if you want to modify the variable without creating new instance of the class or extending it)

<?php
class MyClass {
    public static $var = 'A Parent Val';

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

echo MyClass::$var;//A Parent Val
MyClass::$var="Replaced new var";
echo MyClass::$var;//Replacced new var
?>
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
  • 1
    I don't think this is what he's looking for. He's looking for a way to change `property` without instantiating an object. – Kemal Fadillah Oct 06 '13 at 02:39
  • Can't static properties be changed? – francisco.preller Oct 06 '13 at 02:45
  • Not outside the class. If a setter method exists, you could call it but if you called a getter on the next line, it would not have that value, since each call is to new staticly "rendered" objects of the class. It might be possible to chain the methods in one call, but I'm not savvy on that approach. – Anthony Oct 06 '13 at 02:52
  • @Anthony [phpFiddle](http://phpfiddle.org/lite/code/75e-x86) as long as the static member is visible it can be accessed outside the class. That question you are linking to is trying to do chains which you cant do with static methods till you have an instance to chain from. – Patrick Evans Oct 06 '13 at 02:58
  • So they are static in the same sense as static variables in functions. Good to know. I still think the OP is looking for a way to overwrite the property of a 3rd party class, but your solution works if the goal is simply to assign a value that persists across new objects / static calls. – Anthony Oct 06 '13 at 04:36
  • Would making the property protected prevent this? Or just force using a setter? – Anthony Oct 06 '13 at 04:37
  • @Anthony, yea if his goal was to modify at runtime then yea i agree with you there that, that cannot be done. As for protected/private would make it unusable outside the class yes, unless you had a public static setter setup – Patrick Evans Oct 06 '13 at 19:53