Reputation: 416
I used the search function but i wasnt able to find an answer to my question, so here it is:
Im building a small game. The game is about starships shooting each other. Each Starship is a class with an inherit point value like this
class Ship {
$public value;
function __construct($value){
$this->value = $value;
}
}
The $value
is needed several times, for the most past once the server resolves gameplay and each Ship is properly instantiated ($ship->value
)
However, occasionally i as well need the value of the various ships without first instanstitating them.
And this is my problem. I know i can access props without instantiation by making them static (Ship::$value
), however by doing that, i am no longer able to access it from inside the Class ("accessing static prop as as non-static")
My question is: What is the best way to set up my class with a $value property so that i can access it as universally as possible, i.e. with and without instantiating the Class ?
Upvotes: 0
Views: 43
Reputation: 72286
I know i can access props without instantiation by making them static (Ship::$value), however by doing that, i am no longer able to access it from inside the Class.
A static property is similar to a global variable. When you change it, it changes for all the objects of the class because there is only one instance of it. It doesn't depend of the presence of an instance because, well, it is a global variable, possibly with the access limited to the objects of its class.
An object property is a property of each instance of the class. You cannot use it (get or set) until you create an object (an instance of the class) that contains it. It doesn't exists alone.
The $value is needed several times, for the most past once the server resolves gameplay and each Ship is properly instantiated ($ship->value) However, occasionally i as well need the value of the various ships without first instanstitating them.
What do you mean by "need the value of the various ships without instantiating them"?
If the ship does not exist yet how do define its value?
I guess what do you mean is a default value for all ships. Or, maybe, you have ship types and each type has a different value (but all ships of the same type have the same value).
This can be easily accomplished by defining one (or more) class variables (or constants) that store the default value of all ships (or of all ships of a certain type).
class Ship {
protected static $defaultValue = 42;
public $value;
public function __construct($value){
$this->value = $value;
}
public static function getDefaultValue()
{
return self::$defaultValue;
}
}
// Use the default value (it's a class property, it always exists)
echo(Ship::getDefaultValue()); // 42
// Instantiate a ship, set a custom value to it
$ship = new Ship(51);
// Use its value
echo($ship->value); // 51
// You can also access the class property $defaultValue using an instance
echo($ship->getDefaultValue()); // 42
Upvotes: 2
Reputation: 9775
I know i can access props without instantiation by making them static (Ship::$value), however by doing that, i am no longer able to access it from inside the Class ("accessing static prop as as non-static")
You can still access static properties from inside the class.
All you have to do is to call them like this self::$value
instead of $this->value
.
Upvotes: 2