1

I am fairly new to PHP OOP, the problem that I am have is that I can't wrap my head around the follow layout of my script:

  • main class is set which sets up the page and extends a mysql class and creates the database connect through the __construct
  • within main class i run a public function which includes() a file and accesses a function that is in that include file
  • within the function that is in the included file i can't seem to access the main class through neither the actual global variable or use $this->blah

does anyone have any pointers or direction. i tried googling it but couldn't come across anything remotely close to what i was trying to do.

it is started with: - works

$gw = new GWCMS();

then inside of the _construct of GWCMS() which GWCMS extends mySQL - works

parent::__construct(DB_HOST,DB_USER,DB_PASSWORD,DB_NAME);
$this->build();

then it calls build() - works

public function build(){
   ...
   $page['content'] = $this->plugins($page['content']);
   ...
   $output = $this->output($theme,$page);
   echo eval('?>' . $output);
}

which calls plugins() - we start having problems

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;
}

the previous code includes inc.php which lists the files to be include:

include(STOCKPLUGINPATH . 'Test/test.php'); 

test.php includes the list of functions. the do_shortcode above accesses the functions without a problem and does the work however i need the following function which is in the test.php to access the $gw->fetchAssoc(); which fetchAssoc is in the parent of gwcms

function justtesting2($attr){
   $config = $gw->fetchAssoc("Select * from gw_config");
   foreach($config as $c){
      echo $c['value'];
   }
}

when i run the script i get

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
4

2 に答える 2

0

ファイルが関数に含まれている場合、ファイルはその関数のスコープからのみアクセスできます。

http://php.net/manual/en/function.include.php#example-136

作成したオブジェクトへの参照をファイルを含む関数に与えるか、ファイルをその関数のスコープにプルしてアクセスする必要があります。

于 2012-06-27T16:18:35.453 に答える
0

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
}
于 2012-06-27T16:20:00.223 に答える