PhP调用一个函数并传递参数来构造

时间:2017-10-23 10:34:56

标签: php laravel laravel-5 laravel-5.5

我有一个社交课程:

  protected void generateKey() {
    try {
        keyStore = KeyStore.getInstance("AndroidKeyStore");
    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        keyGenerator = KeyGenerator.getInstance(
                          KeyProperties.KEY_ALGORITHM_AES, 
                          "AndroidKeyStore");
    } catch (NoSuchAlgorithmException | 
                    NoSuchProviderException e) {
        throw new RuntimeException(
        "Failed to get KeyGenerator instance", e);
    }

    try {
        keyStore.load(null);
        keyGenerator.init(new  
           KeyGenParameterSpec.Builder(KEY_NAME,
                KeyProperties.PURPOSE_ENCRYPT |
                KeyProperties.PURPOSE_DECRYPT)
                .setBlockModes(KeyProperties.BLOCK_MODE_CBC)
                .setUserAuthenticationRequired(true)
                .setEncryptionPaddings(
                     KeyProperties.ENCRYPTION_PADDING_PKCS7)
                        .build());
        keyGenerator.generateKey();
    } catch (NoSuchAlgorithmException | 
              InvalidAlgorithmParameterException
            | CertificateException | IOException e) {
        throw new RuntimeException(e);
    }

在另一个函数中,我正在调用这样的外观:

protected $id;

public function __construct($request, $id)
{
    Log::info('Processing...', ['request' => $request, 'id' => $id]);
    try {
        $client = new Client();
        $url =  sprintf($request);
        $response = $client->get($url);
        $json = json_decode((string) $response->getBody(), true);
        return $json;
    } catch (ClientException $exception) {
        $responseBody = $exception->getResponse()->getBody(true);
        Log::error($responseBody, ['entity_id' => $id]);
    }
}
public function wikipedia($wikipedia_url, $id)
{
  dd($json);
    try {
        $wikipedia_array = $json['parse']['text'];
        $wikipedia_array = array_slice($wikipedia_array, 0, 9);
        $wikipedia_array = implode($wikipedia_array, ',');
        Log::info('Processed Wikipedia for', ['entity_id' => $id]);
        return $wikipedia_array;
    } catch (Exception $e) {
        Log::error('Wikipedia:', ['message' => $e->getMessage(), 'entity_id' => $id]);
    }
}

但是我得到了这个:

  

类型错误:函数App \ Helpers \ Social :: __ construct()的参数太少,0在第35行传递/home/vagrant/liveandnow/app/Providers/SocialServiceProvider.php,正好是预期的2

任何人都可以向我解释如何调用方法,将参数传递给它但是也将它们传递给构造?

这是我的门面:

    $id = $entity->id;
    $wikipedia_id = $entity->wikipedia;
    if (!empty($wikipedia_id)) {
        $wikipedia_url =  'http://en.wikipedia.org/w/api.php?action=parse&prop=text&section=0&disablelimitreport=1&format=json&page='.$wikipedia_id;
        $wikipedia_html = Social::wikipedia($wikipedia_url, $id);
        Log::info('Wikipedia ok for', ['entity_id' => $id]);
    }

和服务提供商:

<?php

namespace App\Facade;

use Illuminate\Support\Facades\Facade;

class Social extends Facade
{
    /**
     * Get the registered name of the component.
     *
     * @return string
     */
    protected static function getFacadeAccessor()
    {
        return 'social';
    }
}

1 个答案:

答案 0 :(得分:0)

错误在于服务提供商。 您可以使用2个参数

定义构造函数

public function __construct($request, $id)

但是在您的服务提供商中,您可以这样称呼它:

public function register()
{
    $this->app->bind('social', function ($app) {
        return new Social;
    });
}

在实例化Social类时需要添加两个参数,例如

return new Social("http://xyz.de", 1);

希望这有帮助。