2010-05-19 29 views
6

So habe ich zwei Klassen wie folgt aus:PHP Zugang Klasse innerhalb einer anderen Klasse

class foo { 
    /* code here */ 
} 
$foo = new foo(); 
class bar { 
    global $foo; 
    public function bar() { 
     echo $foo->something(); 
    } 
} 

ich die Methoden von foo innerhalb aller Methoden bar zugreifen möchten, ohne es in jeder Methode in bar zu erklären, wie folgt aus:

class bar { 
    public function bar() { 
     global $foo; 
     echo $foo->something(); 
    } 
    public function barMethod() { 
     global $foo; 
     echo $foo->somethingElse(); 
    } 
    /* etc */ 
} 

Ich möchte es auch nicht verlängern. Ich habe versucht, das var-Schlüsselwort zu verwenden, aber es schien nicht zu funktionieren. Was mache ich, um auf die andere Klasse "foo" in allen Methoden von bar zuzugreifen?

Antwort

7

Sie mögen dies auch tun könnte:

class bar { 
    private $foo = null; 

    function __construct($foo_instance) { 
     $this->foo = $foo_instance; 
    } 

    public function bar() { 
     echo $this->foo->something(); 
    } 
    public function barMethod() { 
     echo $this->foo->somethingElse(); 
    } 
    /* etc, etc. */ 
} 

Später könnten Sie tun:

$foo = new foo(); 
$bar = new bar($foo); 
+0

Niemals daran gedacht, es als Parameter zu übergeben; es funktioniert jetzt. Vielen Dank! –

+0

@arxanas: Sie sind willkommen :) – Sarfraz

+3

FYI, das ist bekannt als Dependency Injection –

4

Machen Sie es zu einem Mitglied der Bar. Versuchen Sie niemals, Globals zu verwenden.

class bar { 
    private $foo; 

    public function __construct($foo) { $this->foo = $foo; } 

    public function barMethod() { 
     echo $this->foo->something(); 
    } 
} 
2

Die kurze Antwort: Nein, es gibt keine Möglichkeit zu implementieren, was Sie wollen.

Noch eine kurze Antwort: Sie arbeiten mit Klassen auf "falsche" Weise. Sobald Sie objektorientiertes Paradigma gewählt haben - vergessen Sie das "globale" Schlüsselwort.

Die richtige Art zu tun, was Sie wollen, ist eine Instanz von foo als Mitglied von bar zu erstellen und seine Methoden zu verwenden. Dies wird delegation genannt.

0

Eine Option ist das automatische Laden Ihrer Klassen. Auch wenn Sie Ihre Klasse eine statische Klasse machen, können Sie es ohne $classname = new classname() nennen:

//Autoloader 
spl_autoload_register(function ($class) { 
$classDir = '/_class/'; 
$classExt = '.php'; 
include $_SERVER['DOCUMENT_ROOT'] . $classDir . $class . $classExt; 
}); 

//Your code 
class bar { 
    private static $foo = null; //to store the class instance 

    public function __construct(){ 
     self::$foo = new foo(); //stores an instance of Foo into the class' property 
    } 

    public function bar() { 
     echo self::$foo->something(); 
    } 
} 

Wenn Sie Ihre Klasse (foo) in eine statische Klasse umwandeln

//Autoloader 
spl_autoload_register(function ($class) { 
$classDir = '/_class/'; 
$classExt = '.php'; 
include $_SERVER['DOCUMENT_ROOT'] . $classDir . $class . $classExt; 
}); 

//Your code 
    class bar { 
     public function bar() { 
      echo foo::something(); 
     } 
    } 
1

Und wenn Ihr nur Fokus die Methoden selbst im Gegensatz zu der Instanz einer anderen Klasse, können Sie x extends y verwenden.

class foo { 
    function fooMethod(){ 
    echo 'i am foo'; 
    } 
} 

class bar extends foo { 
    function barMethod(){ 
    echo 'i am bar'; 
    } 
} 

$instance = new bar; 
$instance->fooMethod(); 
Verwandte Themen