Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/268.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php $this->;类别->;函数():它是如何工作的?_Php_Oop - Fatal编程技术网

Php $this->;类别->;函数():它是如何工作的?

Php $this->;类别->;函数():它是如何工作的?,php,oop,Php,Oop,我已经用PHP编程好几年了,最近才开始研究面向对象的代码。现在我了解了课程和其他内容: class Myclass { public function __construct() { } } 所有这些好东西。。。我还了解如何创建函数和调用我的index.php: $someVar = new Myclass; 有一件事我一直在努力理解,那就是我最近研究了codeigniter,我喜欢它的一个方面,我想尝试在不实际使用codeigniter的情况下完成同样的事情 在C

我已经用PHP编程好几年了,最近才开始研究面向对象的代码。现在我了解了课程和其他内容:

class Myclass {

      public function __construct() {
      }
}
所有这些好东西。。。我还了解如何创建函数和调用我的index.php:

$someVar = new Myclass;
有一件事我一直在努力理解,那就是我最近研究了codeigniter,我喜欢它的一个方面,我想尝试在不实际使用codeigniter的情况下完成同样的事情

在CodeIgniter中,他们的变量$this似乎是他们的类变量。但是通过使用它,您可以同时从多个类调用。。例如:

$this->load->module(); which is in one class file..
$this->db->query(); which is in another class file.
我在谷歌搜索了几天,试图找出如何做同样的事情,每个类之间都有相关性,这样我就可以在我的项目中运行$this->class\u name->function\u name,而不是为每个类创建一个新变量,或者为了一个干净的索引文件,在单个类文件中包含每个函数


非常感谢您提供的任何信息(除了购买这本书-因为这对我来说不是一个选项),我现在将向您表示感谢(并且可能会在以后再次感谢您,只是为了更好的衡量)。

加载
db
引用是类属性,它们本身就是public
模块()的其他对象
query()
方法。比如说

class MyClass
{
    /**
     * @var CI_DB
     */
    private $db;

    /**
     * @var Loader
     */
    private $load;

    public function __construct(CI_DB $db, Loader $load)
    {
        $this->db = $db;
        $this->load = $load;

        // PHP also allows you to add arbitrary object properties
        // that receive a "public" visibility
        // Please note, this is not a good practice
        $this->foo = new Foo;
    }

    public function someMethod()
    {
        $this->load->module();
        $this->db->query();

        // You can also use foo even though it is not defined
        // in the class properties
        $this->foo->something();
    }
}

像我们那样添加
foo
属性是危险的。因为它接收
公共
可见性,所以可以在类的外部对其进行更改

$class = new MyClass($db, $load);
$class->foo = new Bar;
$class->someMethod(); // will break if Bar does not contain a something() method

我一直在读你和菲尔的评论。首先,您不能在
index.php
上使用
$this
$此
只能在对象的上下文中使用。所以你可以

$someVar = new Myclass;
...
$someVar->db->something();
…相反

我不完全确定你所说的“阅读类”是什么意思,但你可以按照Phil的指示将成员分配给
MyClass

class MyClass {
   private $inj;

   function __construct(Injected $inj) {
      $this->injected = $inj;
   }
}
请注意,
private$inj
声明不是强制性的,但是跳过它是危险的。添加的任何未声明成员都会自动
公开
,如果您依赖magical get,这可能会影响您。我会申报你需要的财产


最后,您需要包括将要使用的所有类定义,或者使用类似于
load\u class()
的函数,或者。如果上面的
Injected
不是声明的类,则在尝试创建类时会出现错误
load_class()
几乎肯定会以某种方式包含类定义。

好吧,我可以理解这一点,但这不是我在代码中读到的。。。至少在CI中,它们有一个名为CI_Controller的类,看起来像:类CI_Controller{public function}构造函数(){$this->load=&load_class('Loader','core');},而加载程序是一个类。。。不是一个私有的$load,$db实际上是CI_db类……这相当懒惰。它利用了PHP对象的一个特性,允许在没有属性声明的情况下添加任意属性。这些运行时属性随
public
可见性范围一起添加。另外,全局函数
load_class()
看起来非常简单,我不需要也不想要load_class()。。我只需要弄清楚如何让我的父类在本例中Myclass包含并读取子类,如my_DB和my_func,然后在my index.php中,通过$this->DB->something或$this->func->something调用它们。我已经用更多信息更新了我的答案。要向类添加属性,我建议您通过如上所示的构造函数注入属性。仅供参考,您通常不会从
index.php
文件中使用
$this
$this
只在类方法中有作用域。@Phil因此我们可以得出结论,CodeIgniter并没有教授最佳实践。对我来说有趣的是通过引用存储加载的类。如果类方法中包含
index.php
,它可能会使用
$this
,但我怀疑这是否与OP的situationindex匹配。php本身就是一个扩展父类的类。。我确实喜欢做注射$inj和私人$inj的想法。。然而,如果我达到我运行10+或者20多个不同类的程度,将所有这些类添加到_构造中会变得非常丑陋。。。因此,虽然我可以使用load_类函数(我可能需要做一些事情才能做到这一点)。。。但问题是如何让it部门遵循最佳实践。。还有我的index.php中的$this->(在本例中是一个类)