![]() |
With this construct you have not declared a variable $address or something similar yet, but rather a kind of new variable type „address“, just a class.
In order to be able to use this class in practice you have to define a variable of the type address. However, in the object-oriented world one does not speak of a variable anymore, but of an object which is instantiated on the basis of a class. The class is just a structural design, a blue print for a real object. In PHP this is done with the following syntax and the operator “new“:
$MyAdress = new Adress;
Afterwards you have applied an object $MyAddress of the class address. With a class address you can instantiate as many objects as you like under different names. This may all be quite familiar to some of you from standard languages like C or Pascal, since there were something similar called „structure“ (in C the keyword „struct“ is used for declaration). Indeed these structures have a lot in common with the classes of object-oriented programming and have been quasi a kind of precursor of classes. But there is a lot more that can be done with classes !
But first we like to cater briefly to the question, how to access variables defined within the class. In OOP terminology one does not speak of variables which have got values, but of elements (=variables) of the class, which have got properties (=values):
$MyAdress->Name = "Bill";
$MyAdress->Street = "Example street 3a";
$MyAdress->ZIP = 42349;
$MyAdress->City = "Wuppertal";
$MyAdress->Country = "Germany";
Please make sure that there is no dollar sign behind the arrow ! Besides elements behave like normal variables, that means, they can allocate properties respectively values. Like in case of variables, the type is not very important in PHP and is administered automatically by PHP. The difference compared with the example with the classic variables $Name, $Street etc. is that $Name was not related to $Street etc. In the example with the object $MyAddress all elements have got a common relation through the class „Address“.