简体   繁体   中英

Access main class from another PHP class

Good morning, I have to do the following exercise, and I have a couple of questions:

To create the vehicles, I would have to do it in the main class, how can I know from the vehicle class with the getVehiclesCreatives () method how many vehicles have I created?

When using the method go with the vehicle1 let's say "100km" How can it be added to the mileage of the vehicle1?

Thank you very much

Create a Vehicle class that has the class methods getVehiclesCreated () and getKmTotals (); as well as the method of getKmRecorridos (). Test the class through an application that performs, at least, the following actions: - Create 3 vehicles - Go with the vehicle 1 - Go with the vehicle 2 - Go with the vehicle 3 - View vehicle mileage1 - View vehicle mileage2 - View vehicle mileage3 - View total vehicle mileage

<html>
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
<?php
include ("./vehiculo.php");

    $vehiculo1 = new vehiculo();
    $vehiculo2 = new vehiculo();
    $vehiculo3 = new vehiculo();

    $vehiculo1 ->andar(100);
    $vehiculo1 ->getKmRecorridos();




?>

Class vehiculo

<?php

class vehiculo {

public static $vehiculosCreados = 0;
public static $kmTotales;
public $kmRecorridos;

function getVehiculosCreados() {
    return $this->vehiculosCreados;
}

function getKmTotales() {
    return $this->kmTotales;
}

function getKmRecorridos() {
    return $this->kmRecorridos;
}

function setVehiculosCreados($vehiculosCreados) {
    $this->vehiculosCreados = $vehiculosCreados;
}

function setKmTotales($kmTotales) {
    $this->kmTotales = $kmTotales;
}

function setKmRecorridos($kmRecorridos) {
    $this->kmRecorridos = $kmRecorridos;
    static $kmTotales;
    $this->kmTotales = $kmRecorridos + $kmTotales;
}

function andar($kms) {

    $kmRecorridos = $kms + $kmRecorridos;
}

function crearVehiculo($vehiculosCreados) {
    $this->$vehiculosCreados++;
}

}

To get the number of classes you instantiated, you should have a shared variable between all of them. You can do this by declaring $vehiculosCreados as a static variable. Then you simply need to increment this variable in the constructor of your class.

To update the number of traveled kilometers, you need to increment the value of $kmRecorridos by $kms . You inverted the variables in your function andar , it should be $kmRecorridos += $kms .

To have the total number of traveled kilometers, you will need to declare $kmTotales as a static variable too!

In order to get the number of instances of your class you need to do two things:

  • In your class's __construct method increment your
    vehiculosCreados property while referencing self instead of
    this .
  • Create a static function that returns the vehiculosCreados
    property that also references self instead of this .

See this question/answer for more info.

class vehiculo {

  public static $vehiculosCreados = 0;
  public static $kmTotales;
  public $kmRecorridos;

  public function __construct() {
    self::$vehiculosCreados++;
  }

  function getVehiculosCreados() {
   return self::$vehiculosCreados;
  }

  ..

}

And you can get the total instances of your class with vehiculo::getVehiculosCreados()

As for adding to the distance you've traveled you seem to have two variables, one to track the specific car's distance and another to track all car's distances? To increment both we can do it in the same function:

public function andar($kms) {
  $this->kmRecorridos += $kms;
  self::$kmTotales += $this->kmRecorridos;
}

And you can access the total with vehiculo::$kmTotales and the specific car's distance with $vehiculo->getKmRecorridos() .

self::$kmTotales will track all of your instances of vehiculo 's distance while kmRecorridos will track only the current object's traveled distance. I may be misunderstanding what you want, though, so let me know if it is!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM