2016-07-05 74 views
4

我想知道如果我可以在父函数中调用子对象。像这样:在父函数中调用子对象

class Parent { 

      public function A() { 
      << I want to call the object here >> 
      // Some code 
      } 
    } 

    Class Child extends Parent { 

      public function B() { 
      // Some code 
      } 
    } 

    $Child = new Child(); 
    $Child -> B(); 

这两个类在不同的文件中。 My Child类正在使用函数B()与我的数据库建立连接。在我父类的A()中,我试图插入填充表单时收到的数据,但我需要连接到数据库,我不知道如何调用该对象。注意:当我在同一个类中有两个函数时,我的代码正在工作。

我没有找到解决办法,所以我会尝试后我的实际代码:

class db_connect extends Model 
{ 
    private $dbname = "..."; 
    private $dbuser = "..."; 
    private $dbpass = "..."; 
    private $dbhost = "..."; 
    public $dbc; 

    public function Connect() 
    { 
     $this->dbc = mysqli_connect($this->dbhost, $this->dbuser, $this->dbpass, $this->dbname); 

    if($this->dbc === false){ 
     die("ERROR: Could not connect. " . mysqli_connect_error());  
    }} 
} 

因此,这是从上面和connect()类儿童被B()。

现在父

class Model 
{ 
     public $query; 
     public $result; 

     public function proccess_data($marca,$model,$pret,$descriere){ 
     << I am trying to make a connection here from config.php using the function Connect() >> 
     $this->query = "INSERT INTO autoturisme (autoturism_id, marca, model, pret, descriere) " . 
    "VALUES (NULL, '$marca', '$model', '$pret', '$descriere')"; 
     $this->result = mysqli_query(<<Also here I would need the connection>>, $this->query) 
    or die(mysqli_error(<<And here>>)); 
     if($this->result == 1){ 
     echo "<br/>Your data were processed"; 
    } else { 
     echo "<br/>We are sorry but an error occurred"; 
    } 
     $this->close_db(); 

} 

mysqli_query我需要一个参数作为mysqli的,这是我的数据库的连接。该参数位于子级,$ dbc,它在Connect()函数中调用:$ this-> dbc。同样适用于mysqli_error。希望这使事情更清晰:)。

回答

2

您不应该从父类的实例中理想地调用孩子的函数。这是不应该做的。

相反,您可以重写子类中的函数,然后从子类的实例中调用该方法。

2

你可以尝试切换它,所以父类与数据库建立连接,并为子类提供访问该连接的方法。这里是伪代码,粗略地显示了这将如何工作。

class Parent { 

    // method to run SQL statement 
    runSQL(SQL) { 
     // connect to database (or do it in constructor) and run the SQL 
    } 
} 

class Child extend Parent { 
      public function B() { 
       ... 
       parent::runSQL("SELECT...") 
      } 
} 
3

考虑到你已经标记了我会咬人。

有没有理由db_connect东西永远扩展名为Model。更何况,没有理由打电话Model这不告诉任何人任何东西,因此这是一个非常糟糕的名称。

其次,据我所知,没有理由让你包装mysqli开始。你通过包装这样的物体获得了什么。 mysqli带有一个开箱即用的面向对象的界面。

最后,当你摆脱那个奇怪的继承树时,你应该注入数据库连接到需要它的类中。例如:

class Car 
{ 
    // why do you need a `$query` member when you are not going to use it? 
    // same for `$result` 

    private $dbConnection; 

    public function __construct(mysqli $dbConnection) 
    { 
     $this->dbConnection = $dbConnection; 
    } 

    public function add($marca, $model, $pret, $descriere) 
    { 
     $query = 'INSERT INTO autoturisme'; 
     $query.= ' (marca, model, pret, descriere)'; 
     $query.= ' VALUES'; 
     $query.= ' (?, ?, ?, ?)'; 

     $stmt = $this->dbConnection->prepare($query); 

     $stmt->bind_param('ssss', $marca, $model, $pret, $descriere); 

     if (!$stmt->execute()) { 
      throw new \Exception('We are sorry but an error occurred'); 
     } 
    } 
} 

$mysqli = new mysqli('localhost', 'user', 'pass', 'dbname'); 

$car = new Car($mysqli); 

try { 
    $car->add('BMW', '325', 'dunnowhatthismeans', 'description?'); 
} catch(\Exception $e) { 
    echo $e->getMessage(); 
} 

另请注意,您的代码很可能容易受到SQL注入的攻击。