Now of course there are ways around the above, one of which is to set an internal private variable as static. Bear in mind that this will be the case throughout ALL instances of myclass that you create.
Code:
<?php
class myclass {
static private $myvar = "starting string";
public function __construct($newvar=null){
if(!is_null($newvar)){
self::$myvar = $newvar;
}
}
public static function myStaticFunction(){
return self::$myvar;
}
}
echo myclass::myStaticFunction();
echo "<br/>";
$class1 = new myclass();
echo myclass::myStaticFunction();
echo "<br/>";
echo $class1->myStaticFunction();
echo "<br/>";
$class2 = new myclass("a string");
echo myclass::myStaticFunction();
echo "<br/>";
echo $class1->myStaticFunction();
?>
Try the above and see what you get. You may note, out of interest, that when the $class2 is initialised it sets the internal static variable to "a string", and then when the LAST $class1->myStaticFunction is called it will echo out "a string", instead of what it did the first time which was "starting string", however we're not changed anything inside the $class1 object.
tip: be careful what you do with statics...