2017-11-11 86 views
-1

對於OOP,我很新,我試圖調用一次構造函數。PHP調用構造函數一次

我有一個具有Guzzle客戶端的構造函數的類,它也在網站上登錄。現在我有兩個其他類:訂單和順序是用構造函數擴展類(我不知道如果我做正確),我這樣做,因爲我認爲這將調用我的構造函數一次,但它仍然創建倍數客戶端

我的代碼如下所示:

RequestController.php:(有這個建築工使得狂飲客戶端)

namespace App\Http\Controllers\Scraper; 

use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
use GuzzleHttp\Client; 
use Symfony\Component\DomCrawler\Crawler; 

class RequestController extends Controller 
{ 
    public static $_instance = null; 
    public $client; 

    public function __construct(){ 
     $headers = [ 
      'user-Agent' => '', 
      'Host' => 'orders.example.com', 
      'Origin' => 'https://orders.example.com', 
      'Referer' => 'https://orders.example.com', 
     ]; 

     $form_params = [ 
      'form_params' => [ 
       'username' => env('USERNAME'), 
       'password' => env('PASSWORD'), 
       'login' => 'true', 
       'language' => 'en' 
      ] 
     ]; 

     $this->client = new Client([ 
      'headers' => $headers, 
      'cookies' => new \GuzzleHttp\Cookie\CookieJar 
     ]); 

     echo 'new client has created <br>'; 

     $this->client->post('https://orders.example.com/', $form_params); 

     return $this->client; 
    } 
} 

OrdersController.php:

<?php 

namespace App\Http\Controllers\Scraper; 

use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
use App\Http\Controllers\Scraper\RequestController; 
use App\Http\Controllers\Scraper\OrderController; 
use Symfony\Component\DomCrawler\Crawler; 
// use GuzzleHttp\Client; 

class OrdersController extends RequestController 
{ 
    public $order_ids = []; 

    public function orders(){ 
     $response = $this->client->post('https://orders.example.com/orders/orders'); 
     $crawler = new Crawler((String) $response->getBody()); 

     $order_ids = $crawler->filterXPath('//tbody[contains(@class, "wide")]')->extract(['_text', 'rel']); 

     foreach($order_ids as $order_id){ 
      $order = new OrderController(); 
      $order->order(substr($order_id[1], 2)); 
     } 
    } 
} 

OrderController.php:

<?php 

namespace App\Http\Controllers\Scraper; 

use Illuminate\Http\Request; 
use App\Http\Controllers\Controller; 
use App\Http\Controllers\Scraper\RequestController; 

class OrderController extends RequestController 
{ 
    private $_delivery_time, 
      $_order_price, 
      $_delivery_price, 
      $_total_price, 
      $_pay_option; 

    public function order($order_id){ 
     $details = [ 
      'form_params' => [ 
       'id' => $order_id 
      ] 
     ]; 

     $response = $this->client->post('https://orders.example.com/orders/details', $details); 

     // echo (string) $response->getBody(); 

     // die(); 

    } 
} 

如果任何人都可以給我一些意見,我將不勝感激,謝謝!!

回答

0

按照文檔:http://php.net/manual/en/language.oop5.decon.php

注:如果子類定義了一個構造函數構造家長不隱式調用。爲了運行父構造函數,需要在子構造函數中調用parent :: __ construct()。如果孩子沒有定義一個構造函數,那麼它可能像父類一樣繼承父類(如果它沒有被聲明爲私有的)。

換句話說,如果你不定義構造函數,類將繼承它的父類的構造

class Animal { 
    public function __construct() { 
     echo "Animal instance"; 
    } 
} 

class Dog extends Animal { 
    public function bark() { 
     echo "woof"; 
    } 
} 

$dog = new Dog(); 

//Output will be Animal instance 

class Animal { 
    public function __construct() { 
     echo "Animal instance"; 
    } 
} 

class Dog extends Animal { 
    public function __construct() { 
     echo "woof"; 
    } 
} 

$dog = new Dog(); 

//Output will be woof