Code Real Estate: Where Properties Define Object Value
Properties, also called Class Member Variables, Attributes, and Fields, are the characteristics of a class. Imagine them as variables placed inside of the class’s curly braces, but not inside of methods themselves.
Properties can be directly initialized or they can be initialized when the object is instantiated through the constructor; we’ll get to constructors later.
Properties can have different visibility based on the keyword in front of them:
- public
- private
- protected
Visibility will also be explored later, but for the time being, private properties are only visible to the object, public properties are visible inside and outside of the object, and protected properties are visible to the object and its children.
PHP — P43: Objects and Classes Intro – Need to link it to proper article
Dog Class
One of the simplest ways to visualize properties is by looking at characteristics of animals. For example, a dog has a specific eye color, height, weight, etc; the dog can shed; the dog can be a specific breed and have a specific fur color. To visualize this, we can create a Dog class and add a few properties to it. For simplicity sake, we’ll keep all properties public.
<?php
class Dog {
public $eye_color = "Brown";
public $dob = "January 30, 2017";
public $does_shed = true;
public $kingdom = "Animalia";
public $phylum = "Mammalia";
public $class = "Mammalia";
public $order = "Carnivara";
public $family = "Canidae";
public $genus = "Canis";
public $species = "Canis Lupus";
public $subspecies = "Canis Lupus Familiaris";
public $breed = "German Shepherd Dog";
public $fur_color = "Black and Tan";
}
Once we instantiate our Dog class, we can reference those properties by asking the object what its fur color is, for example.
<?php
$gs_dog = new Dog();
echo $gs_dog->dob;
echo "<br>";
echo $gs_dog->fur_color;
echo "<br>";
$gs_dog->fur_color = "Black";
echo $gs_dog->fur_color;
Whenever we instantiate the Dog class, we have default characteristics that it comes with. You can already see foreshadowing in the making on why we need constructors: hint, not every dog has black and tan fur color. But, it’s our program and we can do with it as we please. We can even modify those traits. In the example above, we changed the dog’s fur color from Black and Tan to just Black. When we print out the dog object’s fur color now, it’s black.
Lamborghini Example
Non-living things can also have characteristics. Think about a vehicle, and while you’re thinking about it, might as well think about something nice, like a Lamborghini. The Lamborghini has specific characteristics, such as:
- color
- height
- width
- weight
These traits are seen in the example below.
<?php
class Car {
public string $color;
public string $make;
public string $model;
public int $year;
public int $fuel_type = 93;
public int $hp;
public int $tq;
public string $transmission = "6 Speed Manual";
public string $vehicle_type = "Coupe";
public float $exterior_height;
public float $exterior_width;
public float $exterior_length;
public string $exterior_um = "in";
public float $weight;
public string $weight_um = "lbs";
}
$lamborghini = new Car();
$lamborghini->color = "Black";
$lamborghini->make = "Lamborghini";
$lamborghini->model = "Diablo";
$lamborghini->year = 1998;
$lamborghini->hp = 518;
$lamborghini->tq = 427;
$lamborghini->exterior_height = 43.5;
$lamborghini->exterior_width = 80.3;
$lamborghini->exterior_length = 177.4;
$lamborghini->weight = 3474;
There’s something different about this example. For one, PHP allows you to declare property types. If you know that a specific property will be, for example, a string, you can specify that it will always be a string; same goes for all other property types.
We also left certain properties as uninitialized; look at color for reference. Why? Because cars come in different colors, and even though the
Verde Mantis Green is my favorite color on the Lamborghini, not all cars come in that color. For our purpose, all vehicles will be measured in inches and pounds, so we initialized the units of measure to in and lbs. Since this is my fantasy world, I’m forcing all cars to have a 6-speed manual transmission.
When we instantiate the Lamborghini object from the Car class, we have quite a few uninitialized properties. If we want to print those properties, we have to set a value for them first. In this case, we specified that we want a Black Lamborghini Diablo with specific HP and TQ (seen in the example above).
Time to access our property values and display them on the screen. You can concatenate each of the properties in an echo statement and see what you receive.
?php
echo $lamborghini->make . " " . $lamborghini->model . ". " .
" HP: " . $lamborghini->hp . ". Weight: " . $lamborghini->weight . " " .
$lamborghini->weight_um;
var_dump( $lamborghini );
It’s also an interesting exercise to var_dump the object. You can see the various components that make up the object.
object(Car)#1 (15) {
…
[“fuel_type”]=>
int(89)
…
}
I’ve removed majority of the object properties above and left one, the fuel type, since PHP has some interesting behavior. If we modify the fuel type and assign it a numeric string, it will still work. For example:
$lamborghini->fuel_type = “89”;
If you var_dump the object again, you still get the exact same result. PHP automatically casts the numeric string to an int for you. You can’t do the same with a non-numeric string.
$lamborghini->fuel_type = “Dino”;
The example above will produce an error.
Fatal error: Uncaught TypeError: Cannot assign string to property Car::$fuel_type of type int
Continue your learning with these articles
USING CLASS BLUEPRINTS TO CONSTRUCT REAL OBJECTS
PHP – P43: OBJECTS AND CLASSES
PHP has been an Object Oriented Language since PHP 5. It’s mature, powerful, and there should be no reason for anyone to hate on it any longer.
Code Real Estate: Where Properties Define Object Value
Properties, also called Class Member Variables, Attributes, and Fields, are the characteristics of a class. Imagine them as variables placed inside of the class’s curly braces, but not inside of methods themselves.
KEEPING CODE GROUNDED IN A SEA OF VARIABLES
Class constants cannot be changed, hence the name constant. They’re declared using the const keyword and are allocated in memory once per class.