OO PHP函数

OO PHP函数,php,oop,class,Php,Oop,Class,我对PHP OOP相当陌生,我遇到的问题是,我无法理解脚本的以下布局: main类用于设置页面、扩展mysql类并通过_构造创建数据库连接 在主类中,我运行一个包含()文件的公共函数,并访问该包含文件中的函数 在包含文件中的函数中,我似乎无法通过实际的全局变量或使用$this->blah访问主类 有人有任何指示或方向吗。我试着在谷歌上搜索,但找不到任何和我想做的事情相近的东西 这是从工作开始的 $gw = new GWCMS(); 然后在GWCMS()的_构造中,GWCMS扩展了mySQL

我对PHP OOP相当陌生,我遇到的问题是,我无法理解脚本的以下布局:

  • main类用于设置页面、扩展mysql类并通过_构造创建数据库连接
  • 在主类中,我运行一个包含()文件的公共函数,并访问该包含文件中的函数
  • 在包含文件中的函数中,我似乎无法通过实际的全局变量或使用$this->blah访问主类
有人有任何指示或方向吗。我试着在谷歌上搜索,但找不到任何和我想做的事情相近的东西

这是从工作开始的

$gw = new GWCMS();
然后在GWCMS()的_构造中,GWCMS扩展了mySQL-works

parent::__construct(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$this->build();
然后调用build()-works

调用plugins()-我们开始遇到问题

public function plugins($content){
   $x = 0;
   if ($handle = opendir(STOCKPLUGINPATH)) {
      while (false !== ($entry = readdir($handle))) {
         if(is_dir(STOCKPLUGINPATH . $entry . '/') && $entry != '.' && $entry != '..'){ 
            if(file_exists(STOCKPLUGINPATH . $entry . '/inc.php')){
               include(STOCKPLUGINPATH . $entry . '/inc.php');
               $content = do_shortcode($content);
            }
         }
      }
      closedir($handle);
   }
   return $content;
}
前面的代码包括inc.php,其中列出了要包括的文件:

include(STOCKPLUGINPATH . 'Test/test.php'); 
php包括函数列表。上面的do_短代码访问函数时没有问题,并且可以正常工作,但是我需要test.php中的以下函数来访问$gw->fetchAssoc();哪个fetchAssoc是gwcms的父级

function justtesting2($attr){
   $config = $gw->fetchAssoc("Select * from gw_config");
   foreach($config as $c){
      echo $c['value'];
   }
}
当我运行脚本时,我得到

Fatal error: Call to a member function fetchAssoc() on a non-object in /home/globalwe/public_html/inhouse/GWCMS/gw-includes/plugins/Test/test.php on line 9

当文件包含在函数中时,它们只能从该函数的作用域进行访问:


您需要将您创建的对象的引用提供给包含该文件的函数,或者将其拉入该函数的作用域以访问它。

编写OOP代码意味着重新构造,以避免将文件和函数的混乱放入任何文件中,天知道什么不是

尝试编写一个类来模拟您想要实现的行为。该类应该包含为您携带数据的属性值,以及帮助该类像您正在建模的对象一样运行的方法

回答你的问题:

class MyClass {
    public $my_property = 4;
    public function MyMethod() {
        include('file.php');
    }
    public function MyOtherMethod() {
        $this; // is accessible because MyOtherMethod
               // is a method of class MyClass
    }
}

// contents of file.php

$variable = 3;

function MyFunction($parameter) {
    global $variable; // is accessible
    $parameter; // is accessible
    $this // is not accessible because it was
          // not passed to MyFunction as a parameter
          // nor was it declared as a global variable

    // MyFunction is not a method of class MyClass,
    // there is no reason why $this would be accessible to MyFunction
    // they are not "related" in any OOP way
    // this is called variable scoping and/or object scoping
}

我们需要查看代码。请仅发布相关部分。
$此
不在该功能的范围内,为什么您的“全局”不起作用。。。可能是因为它也不在全局范围内,而是在另一个范围内。如果没有一个(最简单的!)代码示例,很难说出来。Global听起来一点都不像OOP。main类不应该扩展mysql类,您应该做的是让一个引导程序加载所需的类,并将依赖项注入或加载您的类到注册表类中,然后将其传递给构造函数。@LawrenceCherone您不认为您将他推向了最深处吗?悬崖下。。。深入到底?不学习OOP原则和学习依赖注入会创造出一种来自地狱的android程序员,他们出生在一个设计模式中,但仍然不理解它或OOP,并创造出最令人痛心的意大利面条和肉丸(即意大利面条代码+OOP对象)。
class MyClass {
    public $my_property = 4;
    public function MyMethod() {
        include('file.php');
    }
    public function MyOtherMethod() {
        $this; // is accessible because MyOtherMethod
               // is a method of class MyClass
    }
}

// contents of file.php

$variable = 3;

function MyFunction($parameter) {
    global $variable; // is accessible
    $parameter; // is accessible
    $this // is not accessible because it was
          // not passed to MyFunction as a parameter
          // nor was it declared as a global variable

    // MyFunction is not a method of class MyClass,
    // there is no reason why $this would be accessible to MyFunction
    // they are not "related" in any OOP way
    // this is called variable scoping and/or object scoping
}