变量无法识别PHP POO


variable not recognized PHP POO

我有一个带有不同类的文件data.php。在另一个文件中,我进行了必要的调用。

<?php
include_once('data.php');
$db= new dataManager(); // here 
class Manager{
//$db= new dataManager(); here doesn't work an error occurs

function savingData($ob )//obj 
{
      $db.saveData($ob);//error undefined variable $db
}

尝试使用->调用php 中带有对象的函数

$db->saveData($ob);

您还需要将$ob传递给管理员类函数

$newobj= new Manager();
$newobj->savingData($ob);

好的,搜索后我记住了全局单词

只需要添加

全局$variable;在每个功能中。

还有效。

<?php
include_once('data.php');
$db= new dataManager(); // here 
class Manager{
function savingData($ob )//obj 
{
  global $db; //<---
  $db.saveData($ob);//error solved
}