Problem with use in PHP

2

I'm developing an application that sends an image to a bucket in amazon and I'm using sdk pure

To send use the following code:

function especifica(){
    require '..\..\lib\aws\aws-autoloader.php';
    use Aws\S3\S3Client;

    define("ACCESS_KEY_AMAZON", "");
    define("SECRET_KEY_AMAZON", "");

    try {
        $clientS3 = S3Client::factory(array(
            'region'   => 'região',
            'version' => 'latest',
            'key'    => ACCESS_KEY_AMAZON,
            'secret' => SECRET_KEY_AMAZON
        ));
        $response = $clientS3->putObject(array(
            'Bucket' => "bucket",
            'Key'    => "profile_picture/".$_POST['idusuario'].".png",
            'SourceFile' => $_POST['hidden_cropped'],
        ));

        die(var_dump($response));
        echo "Objeto postado com sucesso, endereco <a href='{$response['ObjectURL']}'>{$response['ObjectURL']}</a>";
    } catch(Exception $e) {
        log_errors( "Edit picture, upload to amazon S3".PHP_EOL."Exception: ".$e);
    }
}

It happens that my API has more than one function in the same file, type change photo, change basic information, cancel subscription, change password and so on.

So I preferred to keep require to run only within the photo change function, but I was getting the error directly:

  

Unexpected error 'use' (T_USE)

After searching I found that namespace use can only be declared no escopo mais externo de um arquivo .

Source: Unexpected 'use' error (T_USE) use autoload

Ok, I put use in the outermost scope, more or less this way:

require '..\..\lib\aws\aws-autoloader.php';
use Aws\S3\S3Client;

function especifica(){    
    define("ACCESS_KEY_AMAZON", "");
    define("SECRET_KEY_AMAZON", "");

    try {
        $clientS3 = S3Client::factory(array(
            'region'   => 'região',
            'version' => 'latest',
            'key'    => ACCESS_KEY_AMAZON,
            'secret' => SECRET_KEY_AMAZON
        ));
        $response = $clientS3->putObject(array(
            'Bucket' => "bucket",
            'Key'    => "profile_picture/".$_POST['idusuario'].".png",
            'SourceFile' => $_POST['hidden_cropped'],
        ));

        die(var_dump($response));
        echo "Objeto postado com sucesso, endereco <a href='{$response['ObjectURL']}'>{$response['ObjectURL']}</a>";
    } catch(Exception $e) {
        log_errors( "Edit picture, upload to amazon S3".PHP_EOL."Exception: ".$e);
    }
}

And it worked, but just wanted to know if it affects my other functions? In the loading time mainly, since the amazon sdk has 6MB.

    
asked by anonymous 26.12.2016 / 20:18

2 answers

2

The use operator serves as an alias for namespaces. There is another purpose for use in which you pass an argument by reference, but it is not the case here so the focus will only be on who asked:

  

And it worked, but just wanted to know if it affects my other functions?   In the loading time mainly, since the amazon sdk has   6MB.

The use operator only creates a "shortcut" (alias). It does not load the target object of the alias. So you do not have to worry about cost of memory, etc. It is obvious that there is a cost regarding the use of the operator, however the objects are not loaded automatically.

What can go wrong in your application is, if the application does not have a good organization, some name conflict may occur, but it is unusual and will only happen if you write the application in a very disorganized way, declaring functions , classes or namespaces with commonly used nomenclature, increasing chances of conflict with third-party systems / libraries you are importing into your project (include, require)

This is one of the purposes of using the namespace. Avoid this problem of role conflict with equal names.

An important detail in your case is that you are applying the alias to the global scope. That is, you can invoke classes that are under the namespace Aws\S3\S3Client without needing to specify the namespace.

Example, in a snippet of code invoking S3Client::factory() . This is only possible because the Aws\S3\S3Client alias has been applied to the global scope base. If you need to invoke a function or class outside of that namespace, you must specify the full or relative path with the appropriate indents.

Particularly I prefer to specify an alias instead of playing for root:

use Aws\S3\S3Client as AWS;

So just call this way: AWS\S3Client::factory() . But that depends a lot on the project. If the project does not use any other namespace, you can play the alias for root as you are doing. Of course, as long as you're aware of name conflicts.

To have no such concerns, just create a pattern for your projects by always organizing them into a proper namespace.




About us obs: I suggest you edit the title and question for something more appropriate.
To better understand how to ask questions: How NOT to ask questions handbook

    
27.12.2016 / 16:08
3

I'm considering that you're dealing with a legacy application and you do not want to adopt the best loading practices so I see you can leave the way that it is without compromising performance but you can also remove the use line and use the% of the object's% by putting the backslash at the beginning, eg:

<?php
    require_once '..\..\lib\aws\aws-autoloader.php';
    $clientS3 = \Aws\S3\S3Client::factory( //...
    
27.12.2016 / 13:17