user431806 user431806 - 4 months ago 35
PHP Question

Being able to access a property with and without class instantiation?

I have a class which has a certain property that i need access to both without and with class instantiation.

I felt smart and declared it as public static after reading the internet and voila, i was able to access the value using :: without actually creating an object of that class.

Now, some hours down the road i need to access the same value, but this time over the course of looping over an array that holds an actual instance of the class and i cant access it....

Please advise

Class Omega(){
public static $value = 500;

$name = "Omega";
$prop = "value";
$cost = $name::$$prop // 500 yay it works

$omega = new Omega();
$ships[] = $omega;

$totalCost = 0;

for ($i = 0; $i < sizeof($ships); $i++){
$totalCost = $totalCost + $ships[i$]->value; // UGH

Notice: Accessing static property Omega::$value as non static in C:\xampp\htdocs\fire\test.php on line 44
Notice: Undefined property: Omega::$value in C:\xampp\htdocs\fire\test.php on line 44


value is still part of the class since it's a static property. You can access it with $ships[$i]::$value.