Come posso passare una classe come parametro nella mia funzionePasso PHP classe come parametro
Finora ho provato
$sc = new SampleClass(); SampleFunction($sc); function SampleFunction(&$refClass) { echo $refClass->getValue(); }
questo è un esempio semplificato di ciò che sto facendo .. In realtà devo fare procedure complesse all'interno di questa funzione di esempio. Non ricevo alcuna risposta dalla funzione di esempio. Che cosa sto facendo di sbagliato? grazie
UPDATE
char.php
class Charss {
var $name=0;
var $hp=500;
var $spd=10;
var $rtime=10;
var $dmg=10;
function __construct($name, $hp, $spd, $rtime , $dmg) {
$this->name = $name;
$this->hp = $hp;
$this->spd = $spd;
$this->rtime = $rtime;
$this->dmg = $dmg;
}
function get_name() {
return $this->name;
}
function set_name($new_name) {
$this->name = $new_name;
}
function get_hp() {
return $this->hp;
}
function set_hp($new_hp) {
$this->hp = $new_hp;
}
function get_spd() {
return $this->spd;
}
function set_spd($new_spd) {
$this->spd = $new_spd;
}
function get_rtime() {
return $this->rtime;
}
function set_rtime($new_rtime) {
$this->rtime = $new_rtime;
}
function get_dmg() {
return $this->get_dmg;
}
function set_dmg($new_dmg) {
$this->dmg = $new_dmg;
}
}
myclass.php
require("char.php");
class Person {
function try_process()
{
$chr1 = new Charss("Player1",500,3,0,50);
$chr2 = new Charss("Player2",500,6,0,70);
while ($chr1->get_hp() > 0 && $chr2->get_hp() > 0)
{
$sth = min($chr1->get_rtime(), $chr2->get_rtime());
if ($chr1->get_rtime() == 0 && $chr2->get_rtime() > 0)
{
exit;
Fight($chr1,$chr2);
$chr1->set_rtime($chr1->get_spd());
}
elseif ($chr2->get_rtime() == 0 && $chr1->get_rtime() > 0)
{
Fight($chr2,$chr1);
$chr2->set_rtime($chr2->get_spd());
}
else
{
Fight($chr1,$chr2); #having trouble with this
$chr1->set_rtime($chr1->get_spd());
}
$chr1->set_rtime($chr1->get_rtime() - $sth);
$chr2->set_rtime($chr2->get_rtime() - $sth);
}
}
function Fight($atk,$def)
{
$def->set_hp($def->get_hp() - $atk->get_dmg());
echo $atk->get_name() . " attacked " . $def->get_name() . " for " . $atk->get_dmg() . " damage";
}
}
Così Im chiamando la funzione try_process su scatto del tasto
Se il tuo aggiornamento è ora completo, il tuo problema è che non puoi semplicemente chiamare 'Lotta ($ atk, $ def)', ma piuttosto devi chiamarlo nel formato '$ this-> Lotta ($ atk, $ def) ', in quanto membro della classe. – mkgrunder
Inoltre, si stava usando la proprietà '$ get_dmg' (invece di' $ dmg') in 'Charss :: get_dmg()', che non esisteva. Ho modificato il codice per usare '$ this', e la proprietà che esiste, e ora funziona per me. :) – mkgrunder
Ho trovato questo sperando di capire come passare una _class_ non una _stanza di una classe_. –