1

I have a called class called ClientPolicy which is like this

    class ClientPolicy {
    var $serverHost="www.example.com";
    var $httpPort = 80;
    var $httpsPort = 443;
    var $appKey;
    var $secKey;
    var $defaultContentCharset = "UTF-8";
}

and another class file name SyncAPIClient which looks like this

class SyncAPIClient{

    function SyncAPIClient(ClientPolicy $clientPolicy) {
        $this->clientPolicy = $clientPolicy;
    }

     function SyncAPIClient($appKey, $appSecret) {
        $this->clientPolicy = new ClientPolicy();
        $this->clientPolicy->appKey=$appKey;
        $this->clientPolicy->secKey=$appSecret;
    }

}

My questions are

1.) If you check the function in SyncAPIClient, you will notice that the ClientPolicy class was passed as a parameter before a variable, what does it really mean? What is the essence of passing a class in function parameter?

2.) I am getting an error "Cannot redeclare SyncAPIClient::SyncAPIClient()" in my script log and the reason is that SyncAPIClient function was called twice in SyncAPIClient class. How can I solve this issue? Is there any better way to write this SyncAPIClient function instead of passing it twice?

The author of this script is nowhere to be found and I am left to fix it.

Prince
  • 117
  • 1
  • 9

3 Answers3

0

1) Here the $clientPolicy variable that is passed to this function, needs be a ClientPolicy instance.

In this way, if the argument that is passed is different from an instance of ClientPolice class, an error is generated.

function SyncAPIClient(ClientPolicy $clientPolicy) {
    $this->clientPolicy = $clientPolicy;
}

https://wiki.php.net/rfc/typed_properties_v2 https://laravel-news.com/php7-typed-properties

2) The error Cannot redeclare SyncAPIClient::SyncAPIClient() is caused because you are trying to declare two functions called SyncAPIClient ().

If in first SyncAPIClient() method you just want save the $clientPolicy in $this->clientPolicy, you can use the magic method __construct. Or just try changing the name of one of the functions, and the problem should be a problem.

class SyncAPIClient{

    __construct(ClientPolicy $clientPolicy) {
        $this->clientPolicy = $clientPolicy;
    }

     function SyncAPIClient($appKey, $appSecret) {
        $this->clientPolicy = new ClientPolicy();
        $this->clientPolicy->appKey=$appKey;
        $this->clientPolicy->secKey=$appSecret;
    }

}

https://www.php.net/manual/en/language.oop5.decon.php http://www.zentut.com/php-tutorial/php-constructor-and-destructor/

Hope this helps!

Rafael Laurindo
  • 474
  • 2
  • 7
0

I would've fix the code you have like this:

class SyncAPIClient
{
    private $clientPolicy = null;

    function SyncAPIClient(ClientPolicy $clientPolicy = null) 
    {
        if($clientPolicy instanceof ClientPolicy){
            $this->clientPolicy = $clientPolicy;
        }else{
            $this->clientPolicy = new ClientPolicy();
        }
    }

    public function setAuthParams($appKey, $appSecret) {
        $this->clientPolicy->appKey=$appKey;
        $this->clientPolicy->secKey=$appSecret;
    }   
}

This way you can instantiate a SyncAPIClient with or without a ClientPolicy.

Without ClientPolicy:

$syncAPI = new SyncAPIClient();
$syncAPI->setAuthParams($apiKey, $apiSecret);

With ClientPolicy:

$clientPolicy = new ClientPolicy();
$clientPolicy->appKey=$appKey;
$clientPolicy->secKey=$appSecret;
$syncAPI = new SyncAPIClient($clientPolicy);
Taha Paksu
  • 15,371
  • 2
  • 44
  • 78
0

When using class and functions in combination like

Rtin::

Functions nested inside that class Rtin should have different names than that class name So you shouldn't have function called rtin However you can call function from outside the class with it's name

From the error you have may be due to:

function you nested in the class or the function outside the class has a duplicate outside the script itself. Like having function mentioned in included function.php file and also mentioned in the script itself so php get confused because function name is written in two php files at the same time

Example of class

class Rtin{

private $data;
private $results;

public function getResultsType(){
    return ........
}
}

To call class use

$q = Rtin::getResultsType($data['name']);

In your example. Adapt it to the example I have provide and review the included files for duplicate function .

Creative87
  • 125
  • 9