调用来自所有其他类的类- PHP。

时间:2022-10-29 13:56:22

So I have a config.php file, wich is included on top of every page on the site before the tag. This file uses __autoload() to get all the php classes I use. Then, after autoloading them I assign the classes to variables like so...

我有一个配置。php文件,在标签前的每个页面上都包含了wich。这个文件使用__autoload()来获得我使用的所有php类。然后,在自动加载之后,我将类分配给这样的变量…

$myclass = new Myclass();
$mysecondclass = new MySecondClass();

When I want to call $mysecondclass in $myclass, I get an undefined variable error. This is of course, because $mysecondclass was not defined before $myclass. How do I fix this where I can define all classes from any other class?

当我想在$myclass中调用$mysecondclass时,我得到一个未定义的变量错误。这是当然的,因为$mysecondclass在$myclass之前没有定义。我如何解决这个问题,我可以从其他类定义所有类?

Thanks for any help:)

感谢任何帮助:)

4 个解决方案

#1


4  

The best OOP approach would be to use a superObject with properties which are objects of other classes.

最好的OOP方法是使用具有属性的超对象,这些属性是其他类的对象。

class Context{
  var $myClass;

  var $myOtherClass;

  var $db;

  //...
}

In your MyClass

在你MyClass

class MyClass{

  var $context;

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

  public function otherFunction(){
    $this->context->myOtherClass->functionFromOtherClass();
  }
}

You should be instantiating these classes using Factory method or any mechanism to manage objects.

您应该使用工厂方法或任何管理对象的机制来实例化这些类。

To initialize MyClass you would implement something like this.

为了初始化MyClass,您将实现如下内容。

ObjectFactory::createObject('MyClass',$context);

#2


0  

$myclass is not Myclass it is an instance of Myclass (an object). Objects are not classes. If you want to use a new instance of MySecondClass inside of an instance of Myclass, just instantiate it in there.

$myclass不是myclass,它是myclass(一个对象)的实例。对象不类。如果您想要在Myclass实例中使用MySecondClass的新实例,只需在其中实例化它。

Your question also deals with scope. You have created these two objects in the global scope. They are not automatically accessible within one another. If you have some object which you will have only one instance of and which you want to be global and accessible by others, you can import it into their scope with the global keyword. However, this is not usually the best way to go about it. Instead, you may want to look up the Singleton Pattern. It gives you a way to ensure that a particular class is only instantiated one time and gives you a way to access the object that has been created as a result of that instantiation.

你的问题也涉及范围。您已经在全局范围内创建了这两个对象。它们不能在彼此之间自动获取。如果您有一个对象,您将只有一个实例,并且您希望它是全局的,并且可以由其他对象访问,您可以使用global关键字将其导入到它们的范围中。然而,这通常不是最好的方法。相反,您可能希望查找Singleton模式。它为您提供了一种方法,以确保特定的类只被实例化一次,并为您提供了访问由于实例化而创建的对象的方法。

#3


0  

The registry pattern is also an easy way to access "global" object anywhere in your code.

注册表模式也是在代码中任何地方访问“全局”对象的简单方法。

You can Take a look at Zend_Registry : http://framework.zend.com/manual/en/zend.registry.using.html

您可以查看Zend_Registry: http://framework.zend.com/manual/en/zend.registry.html。

$myclass = new Myclass();
$mysecondclass = new MySecondClass();
Zend_Registry::set('mysecondclass', $mysecondclass);
$myclass->doSomething();

class Myclass
{
    public function doSomething()
    {
        // use $mysecondclass via the registry
        Zend_Registry::get('mysecondclass')->methodFromSecondClass();
    }
}

class MySecondClass
{
    public function methodFromSecondClass()
    {
        return true;        
    }
}

#4


0  

According to how your classes works, think about extending them... Extending MyClass to MySecondClass automatically gives access to parent properties / functions.

根据你的课程如何运作,考虑扩展它们……将MyClass扩展到MySecondClass会自动提供对父属性/函数的访问。

MyClass

MyClass

class MyClass{
    function myFunction(){
         // code here
    }
}

MySecondClass

MySecondClass

class MySecondClass extends MyClass{
    function __construct(){
         $this->myFunction();
    }
}

#1


4  

The best OOP approach would be to use a superObject with properties which are objects of other classes.

最好的OOP方法是使用具有属性的超对象,这些属性是其他类的对象。

class Context{
  var $myClass;

  var $myOtherClass;

  var $db;

  //...
}

In your MyClass

在你MyClass

class MyClass{

  var $context;

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

  public function otherFunction(){
    $this->context->myOtherClass->functionFromOtherClass();
  }
}

You should be instantiating these classes using Factory method or any mechanism to manage objects.

您应该使用工厂方法或任何管理对象的机制来实例化这些类。

To initialize MyClass you would implement something like this.

为了初始化MyClass,您将实现如下内容。

ObjectFactory::createObject('MyClass',$context);

#2


0  

$myclass is not Myclass it is an instance of Myclass (an object). Objects are not classes. If you want to use a new instance of MySecondClass inside of an instance of Myclass, just instantiate it in there.

$myclass不是myclass,它是myclass(一个对象)的实例。对象不类。如果您想要在Myclass实例中使用MySecondClass的新实例,只需在其中实例化它。

Your question also deals with scope. You have created these two objects in the global scope. They are not automatically accessible within one another. If you have some object which you will have only one instance of and which you want to be global and accessible by others, you can import it into their scope with the global keyword. However, this is not usually the best way to go about it. Instead, you may want to look up the Singleton Pattern. It gives you a way to ensure that a particular class is only instantiated one time and gives you a way to access the object that has been created as a result of that instantiation.

你的问题也涉及范围。您已经在全局范围内创建了这两个对象。它们不能在彼此之间自动获取。如果您有一个对象,您将只有一个实例,并且您希望它是全局的,并且可以由其他对象访问,您可以使用global关键字将其导入到它们的范围中。然而,这通常不是最好的方法。相反,您可能希望查找Singleton模式。它为您提供了一种方法,以确保特定的类只被实例化一次,并为您提供了访问由于实例化而创建的对象的方法。

#3


0  

The registry pattern is also an easy way to access "global" object anywhere in your code.

注册表模式也是在代码中任何地方访问“全局”对象的简单方法。

You can Take a look at Zend_Registry : http://framework.zend.com/manual/en/zend.registry.using.html

您可以查看Zend_Registry: http://framework.zend.com/manual/en/zend.registry.html。

$myclass = new Myclass();
$mysecondclass = new MySecondClass();
Zend_Registry::set('mysecondclass', $mysecondclass);
$myclass->doSomething();

class Myclass
{
    public function doSomething()
    {
        // use $mysecondclass via the registry
        Zend_Registry::get('mysecondclass')->methodFromSecondClass();
    }
}

class MySecondClass
{
    public function methodFromSecondClass()
    {
        return true;        
    }
}

#4


0  

According to how your classes works, think about extending them... Extending MyClass to MySecondClass automatically gives access to parent properties / functions.

根据你的课程如何运作,考虑扩展它们……将MyClass扩展到MySecondClass会自动提供对父属性/函数的访问。

MyClass

MyClass

class MyClass{
    function myFunction(){
         // code here
    }
}

MySecondClass

MySecondClass

class MySecondClass extends MyClass{
    function __construct(){
         $this->myFunction();
    }
}