Felhasználói eszközök

Eszközök a webhelyen


oktatas:programozas:php:php_oop:objektumok_toembben

< PHP OOP

Objektumok tömbben

Egyszerűen

index.php
<?php 
 
class Employee {
    public $id;
    public $name;
    public $city;
    public $salary;
    public function __construct($id, $name, $city, $salary) {
        $this->id = $id;
        $this->name = $name;
        $this->city = $city;
        $this->salary = $salary;
    }
}
 
 
$employees = [
    new Employee(1, "Dór Tamás", "Szeged", 2875000),
    new Employee(2, "Ber Ferenc", "Szolnok", 2375000)
];
 
 
print_r($employees);

Futtatás:

php -S localhost:8000 -t .

Kontrollerben

EmployeeController.php
class Employee {
    public $id;
    public $name;
    public $city;
    public $salary;
    public function __construct($id, $name, $city, $salary) {
        $this->id = $id;
        $this->name = $name;
        $this->city = $city;
        $this->salary = $salary;
    }
}
 
 
class EmployeeController extends Controller
{
    public function getEmployees() {
        $employees = [
            new Employee(1, "Dór Tamás", "Szeged", 2875000),
            new Employee(2, "Ber Ferenc", "Szolnok", 2375000)
        ];
        return view('home')->with('employees', $employees);
    }
 
}
oktatas/programozas/php/php_oop/objektumok_toembben.txt · Utolsó módosítás: 2022/03/19 12:41 szerkesztette: admin