简体   繁体   English

从另一个PHP类访问主类

[英]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? 要创建车辆,我必须在主类中完成,如何通过getVehiclesCreatives()方法从车辆类中知道我创建了多少辆车?

When using the method go with the vehicle1 let's say "100km" How can it be added to the mileage of the vehicle1? 当使用方法与车辆1一起行驶时,我们说“ 100公里”如何将其添加到车辆1的行驶里程中?

Thank you very much 非常感谢你

Create a Vehicle class that has the class methods getVehiclesCreated () and getKmTotals (); 创建一个具有类方法getVehiclesCreated()和getKmTotals()的Vehicle类。 as well as the method of getKmRecorridos (). 以及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 通过至少执行以下操作的应用程序测试班级:-创建3辆汽车-与汽车一起行驶1-与汽车一起行驶2-与汽车一起行驶3-查看车辆行驶里程1-查看车辆行驶里程2-查看车辆行驶里程3 -查看车辆总里程

<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. 您可以通过将$vehiculosCreados声明为静态变量来完成此操作。 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 . 要更新行驶的公里数,您需要将$kmRecorridos的值增加$kms You inverted the variables in your function andar , it should be $kmRecorridos += $kms . 您将函数andar的变量取反,应该是$kmRecorridos += $kms

To have the total number of traveled kilometers, you will need to declare $kmTotales as a static variable too! 要获得行驶的总公里数,您还需要将$kmTotales声明为静态变量!

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 在类的__construct方法中,将您的
    vehiculosCreados property while referencing self instead of vehiculosCreados属性,同时引用self而不是
    this . this
  • Create a static function that returns the vehiculosCreados 创建一个返回vehiculosCreados的静态函数
    property that also references self instead of this . 还引用self而不是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() 您可以使用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() . 您可以使用vehiculo::$kmTotales访问总计,并使用$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. self::$kmTotales将跟踪您所有vehiculo距离的实例,而kmRecorridos将仅跟踪当前对象的行进距离。 I may be misunderstanding what you want, though, so let me know if it is! 不过,我可能会误解您想要什么,所以请告诉我它是否是!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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