Cannot redeclare Laravel's __() function when using oscarotero/gettext translation library - php

I have a set of translations in PHP array. Using Oscarotero's gettext library, I'm getting an error of:
"Cannot redeclare __() (previously declared in
D:\LocaleTesting\vendor\laravel\framework\src\Illuminate\Foundation\helpers.php:907)"
when the execution of the code $t->register()
$aTranslation = Translations::fromJsonFile(public_path() . '/locale/'.$sLocale.'/LC_MESSAGES/admin.json');
$oTranslator = new Translator();
$oTranslator->loadTranslations($aTranslation);
$oTranslator->register();
Also, I've search that this error only occur when your Laravel version is 5.4 and above. Any help will do. Thank you! Please Oscarotero/gettext's github for more information about the library.

It seems this is a know issue with the library:
https://github.com/oscarotero/Gettext/issues/180
One way around it is loading the translator functions before laravel helpers are loaded (solution from the issue above):
I'm using the package with a require before vendor/autoload.php on public/index.php and artisan command.
# public/index.php
require __DIR__.'/../vendor/gettext/gettext/src/translator_functions.php';
require __DIR__.'/../vendor/autoload.php';
# artisan
require __DIR__.'/vendor/gettext/gettext/src/translator_functions.php';
require __DIR__.'/vendor/autoload.php';

Related

Is it possible to autoload dd() and or other dev tools at the IDE level not project

I was working in a scratch file and tried to use a non native function (muscle memory) and of cause it threw an error
The PHP Fatal error: Uncaught Error: Call to undefined function dd() in .../.scratch.php
What I wanted to know is can I make dd() for example available globally across all projects/scratch files without having to autoload it myself.
Essentially you would set a PSR-4 autoload path or directory to include.
After exploring the settings I didn't come up with a way of doing it so this is what I did instead.
composer global require --dev symfony/var-dumper
<?php
include $_SERVER['APPDATA']. '/Composer/vendor/autoload.php';
$var = '';
dd($var);
I just complete the answer above
To use 'vendor' projects and components in your phpstorm scratch file
include the file vendor/autoload.php
and for the example to check a Symfony component... :
require '/path/to/my/project/app/vendor/autoload.php';
use Symfony\Component\Intl\Currencies;
\Locale::setDefault('sv');
$res = Currencies::getSymbol('SEK');
print_r($res);

Change namespace class autoload to manual require_once PHP

I want to use namespaces class to manual one without using autoload.php to be included. Because I don't want to use all function the class.
I am using this project https://github.com/codenix-sv/coingecko-api to get it's function in my php function.
In the example of using is like this
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
$client = new CoinGeckoClient();
$data = $client->ping();
But I want to change it to require_once. So I put all src folder in my php folder and create this to call the function
require_once 'libs/Api/CoinGeckoClient.php';
$client = new Codenixsv\CoinGeckoApi\CoinGeckoClient;
$data = $client->simple();
First I got this error when trying to access the page.
Fatal error: Uncaught Error: Class 'GuzzleHttp\Client' not found in
C:\xampp\htdocs\te.st\libs\Api\CoinGeckoClient.php:35
Then I try to remove the line "use GuzzleHttp\Client" in CoinGeckoClient.php file.
And got with this error
Fatal error: Uncaught Error: Class 'Codenixsv\CoinGeckoApi\Client' not
found in C:\xampp\htdocs\te.st\libs\Api\CoinGeckoClient.php:35
Is there any way to just use the "simple" function of coingecko only in my php file.
https://github.com/codenix-sv/coingecko-api/blob/master/src/Api/Simple.php
Here is the way I fix this.
load in composer.json like
{
"require": {
"codenix-sv/coingecko-api": "^1.0",
"guzzlehttp/guzzle": "~6.0"
}
}
then do composer update in command window.
In my php file. make sure
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
is placed in top of file. Then do the rest.
Thanks all
That package is prepared to works with composer.
Composer delivered autoloader, to make you work simplier.
If you remove use GuzzleHttp\Client line from CoinGeckoClient.php, there will be no way to send request to the server.
The best option is include composer autoload in your project file, it means you should:
Create composer.json file for you project
Add required library dependency using command: composer require guzzlehttp/guzzle
Add require library dependency using command: composer require codenix-sv/coingecko-api
Inside your project file add folowing line:
require_once(dirname(__FILE__) . '/vendor/autoload.php');
use Codenixsv\CoinGeckoApi\CoinGeckoClient;
$client = new CoinGeckoClient();
$data = $client->ping();
In other way, that will be necessarily to import all files manually. And of course, you haven't to forget about imports for Guzzle client.

How Can I use two "use" keyword on same page php

I am using two libraries on the same page. One is for pdf generation and one is for sending emails. But, this giving me error 500 - llease advise .
After some debugging, I found that it's phpmailer mails not working because of dompdf:
require_once 'lib/dompdf/vendor/autoload.php';
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\Exception;
require 'lib/phpmailer/vendor/autoload.php';
use Dompdf\Dompdf;
class Pdf extends Dompdf{
public function __construct(){
parent::__construct();
}
}
By using alias:
use \PHPMailer\PHPMailer\{PHPMailer as mailerClass, Exception as mailerException}; // PHP 7+
I think the problem in your code is the duplicate autoloader. The best way is when you install both packages over composer and use the composer autoloader.
In both libs you can find an example how to install them over composer.
composer require dompdf/dompdf
composer require phpmailer/phpmailer
Then you have to include the autoloader for composer.
require 'vendor/autoload.php';
After that you can use the autoloader to load all packages. In the DomPDF you find good examples how to use.
https://github.com/dompdf/dompdf
500 errors are a bit hard to find. You should enable your error logs and check directly your logs. In the log a php error should be shown.

namespace class not found

I want to use this php library "Math-php-master" in my php script.
it is using namespaces.
My script is outside the library folder and the folder for the library is "MathPHP"
There is a php file "Finance.php" which I want to access.
path is "MathPHP/Finance.php". finance.php is using namespace MathPHP
and there is function irr() inside Finance.php which I need.
My code for accessing in my script is below:
include 'MathPHP/Finance.php';
use MathPHP\Finance;
$s = new MathPHP\Finance;
$val = $s->irr(array);
but when I run the script it is giving error "Uncaught Error: Class 'MathPHP\Finance' not found"
I tried everything, I tried the code given on github by the author but it is not working at all
link to the library on github: https://github.com/markrogoyski/math-php
Please help me.
thanks
You have not gone through the effort of readying the readme on how to use the library. In PHP most projects use composer and its autoloading to load classes.
Install composer if you do not have it https://getcomposer.org/doc/00-intro.md#installation-linux-unix-osx
In short as the documentation suggests do
php composer.phar require markrogoyski/math-php:0.*
And in your code
require_once(__DIR__ . '/vendor/autoload.php');
use MathPHP\Finance;
// the rest of the code here

Using Unirest php lib that has a namespace

I'm trying to use unirest, a new php lib for making rest calls.
I'd like to place it in a system-wide directory above my project. I then include it:
require_once ('../unirest-php-master/lib/Unirest/Unirest.php');
loads fine. Then I use it per the readme:
$response = Unirest::post(CSWA_URL ....
I get Fatal error: Class 'Unirest' not found in ...hello_world/sign_start.php on line 23
I then try to use the namespace (see the library's code. They use a Namespace Unirest statement before declaring the Unirest class.)
$response = Unirest\Unirest::post(CSWA_URL ....
I got further. Now: Fatal error: Class 'Unirest\HttpMethod' not found in ....unirest-php-master/lib/Unirest/Unirest.php on line 26 -- This is an error in the library code!
Q: Did I do something wrong? Did the authors of Unirest make a mistake? Do I have to place the library in ./lib? What's the best fix?
It looks like the Unirest code in Unirest.php relies on autoloading code from the two other files in the unirest lib directory (HttpMethod.php and HttpResponse.php).
The author suggests installing the package using composer, if you were to do that composer would add the Unirest namespace to the autoloader.php script it generates. From there you need to require the autoload.php file at the top of your script and it will handle loading classes that aren't defined.
Alternatively, if you don't want to use composer, I would just require the other two files in the unirest lib directory at the top of your script as well.

Categories