• No se han encontrado resultados

ANÁLISIS DE TENDENCIAS Y DEMANDA DE SUELO

B. MEMORIA EXPLICATIVA DE PROYECTO

6 ANÁLISIS DE TENDENCIAS Y DEMANDA DE SUELO

When we try to create an object that has a constructor method, we run the risk of our code breaking if we don’t pass a value to the constructor. In order to avoid this risk, we can define a default value for the properties that we would like to set through the constructor. The default value may be the most reasonable choice for the property, zero, an empty string, or even a null.

If we use a null as the default value, we can use a condition to assess if a value was passed and then, only in that case, assign the value to the property.

In the example below, we give a default value of null to the$modelproperty and, only if a value is passed to the constructor, we assign this value to the property. In any other case, the$modelproperty has a default value of "N/A"string.

class Car {

// The $model property has a default value of "N/A"

private $model = "";

// We don't have to assign a value to the $model property

// since it already has a default value

public function __construct($model = null) {

if($model) {

$this -> model = $model; }

}

public function getCarModel() {

return ' The car model is: ' . $this -> model; }

}

// We create the new Car object

// without passing a value to the model

$car1 = new Car();

echo $car1 -> getCarModel();

Even though we created the object without passing a value to the model property, we didn’t cause an error because the model property in the constructor has a default value of null.

Result:

The car model is: N/A

On the other hand, let’s see what happens when we define the model once we create the object. In the example below, we assign the value “Mercedes” to the $model property as soon as we create the object.

class Car {

private $model = '';

// The constructor

public function __construct($model = null) {

if($model) {

$this -> model = $model; }

}

public function getCarModel() {

return ' The car model is: ' . $this -> model; }

}

// We create the new Car object

// with the value of the model

$car1 = new Car('Mercedes');

echo $car1 -> getCarModel(); Result:

The car model is: Mercedes

Magic constants

In addition to magic methods, the PHP language offers several magic constants.

For example, we may use the magic constant__CLASS__(magic constants are written in uppercase letters and prefixed and suffixed with two underlines) in order to get the name of the class in which it resides.

Let’s take a look at the following example in which we use the__CLASS__ magic constant in the getter method:

class Car {

private $model = '';

// The constructor

public function __construct($model = null) {

if($model) {

$this -> model = $model; }

}

public function getCarModel() {

// We use the __class__ magic constant

// in order to get the class name

return " The <b>" . __class__ . "</b> model is: " . $this -> model; }

}

$car1 = new Car('Mercedes');

echo $car1 -> getCarModel();

Result:

The Car model is: Mercedes

Other magic constants that may be of help are:

__LINE__to get the line number in which the constant is used.

__FILE__to get the full path or the filename in which the constant is used. __METHOD__to get the name of the method in which the constant is used.

Conclusion

In this chapter, we have learned about magic methods and constants, and how to use a constructor to set the values of properties as soon as we create objects out of classes. In the next chapter, we will learn aboutinheritance, a principle of object oriented programming that can save us from repeating ourselves.

Let’s practice what we have just learned

Let’s return to theUserclass that we developed in the previous chapters. However, instead of using setter methods, we will set the values for the first and last name through the constructor.

This is the user class with the private properties of $firstNameand$lastName:

class User {

private $firstName;

private $lastName; }

5.1 Add to the class a constructor method to set a value to the$firstNameproperty as soon

as the object is created.

So far in the chapter, we have used the constructor method to set the value of a single property, but we can use a constructor in order to set the values of more than one property. In our exercise, we will use the constructor method to set the value of the$firstNameas well as the$lastName.

5.2 Add to the constructor the ability to set the value of the$lastNameproperty (remember

that you can pass to a method more than parameter).

5.3 Add to the class agetFullName()public method that returns the full name.

5.4 Create a new object,$user1, and pass to the constructor the values of the first and last

name. The first name is “John” and the last name is “Doe” (you may choose your preferred combination of first and last name).

5.5 Get the full name, and echo it to the screen.

programming

One of the main advantages of object-oriented programming is the ability to reduce code duplication with inheritance. Code duplication occurs when a programmer writes the same code more than once, a problem that inheritance strives to solve. In inheritance, we have a parent class with its own methods and properties, and a child class (or classes) that can use the code from the parent. By using inheritance, we can create a reusable piece of code that we write only once in the parent class, and use again as much as we need in the child classes.

How can a class inherit the code of another class?