Portál AbcLinuxu, 12. května 2025 07:36
class Foo { public $aaa = false; public $bbb = false; function __constructor() { ... ... } } $foo = new Foo; echo $foo->aaa(); // ale false prostě nedostanuNeporadí někdo? Díky
Řešení dotazu:
echo $foo->aaa;
, ale uvidíš prd, neboť z false vyleze prázdný řetězec. Zkus var_export($foo->aaa);
.
class Omg { private $foo; private $bar; private function foo($omg) { if ($omg) { return 42; } return 23; } function method1() { $this->foo = $this->foo(true); } function method2() { $this->bar = $this->foo(false); } } $omg = new Omg; $omg->method1(); $omg->method2();
class Omg { private $tmp; private $foo; private $bar; private $bar2; private function foo() { $this->method1('10'); $this->foo = $this->tmp; $this->method1('20'); $this->bar = $this->tmp; $this->method1('30'); $this->bar2 = $this->tmp; } function method1($res) { $this->tmp = $res; } } $omg = new Omg; $omg->foo; //10 $omg->bar; //20 $omg->bar2; //30
class Omg { private $tmp; private $foo; private $bar; private $bar2; private function __construct() { $this->method1('10'); $this->foo = $this->tmp; $this->method1('20'); $this->bar = $this->tmp; $this->method1('30'); $this->bar2 = $this->tmp; } function method1($res) { $this->tmp = $res; } } $omg = new Omg; $omg->foo; //10 $omg->bar; //20 $omg->bar2; //30
class Omg { private $tmp; private $foo; private $bar; private $bar2; private function __construct() { $this->foo = $this->method1('10'); $this->bar = $this->method1('20'); $this->bar2 = $this->method1('30'); } function method1($res) { return $res; } } $omg = new Omg; $omg->foo; //10 $omg->bar; //20 $omg->bar2; //30
class Omg { private $tmp; private $foo; private $bar; private $bar2; private function __construct() { $this->foo = $this->method1('1', '1000000'); $this->bar = $this->method1('1000001', '2000000'); $this->bar2 = $this->method1('2000001', '3000000'); } function method1($res1, $res2) { # Načtu z mysql řádky od $res1 do $res2 a výsledek uložím do $ret. # některé sloupce v mysql obsahují data o více jak 500 znacíchBabyčka mi říkala, nevracej to return-em, nebo jednoho krásného dne u dveří někdo zazvoní a nakope tě do prdele, a jestli nevíš kdo to bude, tak ti to řeknu, bude to serverreturn $ret; } }
function hello() { return array('foo', 'bar'); } list($a, $b) = hello();nebo
function hello(& $foo, & $bar) { $foo = 'foo'; $bar = 'bar'; } hello($a, $b);V obou případech bude nakonci platit:
$a == 'foo' && $b == 'bar'
Tiskni
Sdílej:
ISSN 1214-1267, (c) 1999-2007 Stickfish s.r.o.