namespace class not found - php

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

Related

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.

Class Amp non found

I'm new on amphp and i'd like to try this very simple code first.
I downloaded amphp with composer for windows and save all folder created inside my project folder.
composer require amphp/http-client
Then my code is:
<?php
require __DIR__ . './autoload.php';
use Amp\Http\Client\HttpClientBuilder;
use Amp\Http\Client\HttpException;
use Amp\Http\Client\Request;
use Amp\Http\Client\Response;
use Amp\Loop;
$stringa = 'http://www.google.it/';
$request = new Request($stringa, "GET");
$location = $request->getHeader("Location");
var_dump($location);
But I get always 'Fatal error: Uncaught Error: Class 'Amp\Http\Client\Request' not found'
Any suggest?
I use wamp local server with php 7.0
Also ,after, I need to yield all the code...
Here are a few things that look for:
Did the autoload.php get included correctly?
I see there is an unnecessary . before /autoload.php.
Did you use the correct class name with the correct namespace?
Did you run composer require amphp/http-client so that the libraries will be installed?
Do the library's files exist inside the vendor/amphp/http-client directory?
If you're on Windows, use \ in the require statement.
Apart from these, I can't think of why the library won't load. I hope this helps.

Laravel 5.4 - How to use EDI Vendor Library

I have installed EDI Library through composer command.
Library Link.
https://github.com/php-edifact/edifact
But in this, they did not give how to register your library in App file.
So can please help me how to register it. They are giving examples of how to use this.
I am just writing simple code.
$x = [1,5,8,9];
$c = new Parser($x);
But the above code is giving an error.
Class 'Parser' not found
Try using new EDI\Parser($x). You need to tell php in what namespace to find the class you are looking for.
Run this command in your command prompt
composer require sabas/edifact
Use in the top of the file like:
namespace EDITest;
use EDI\Parser;

PHP: Cannot reach debug breakpoint with Psysh

I am trying to insert breakpoints into my code via Psysh. I followed the Psysh documentation and installed it successfully via composer. I have composer and psysh available in any shell. Per the documentation I inserted a breakpoint into my code:
<?php
function hello(){
echo 'Hello from Inside the Function';
eval(\Psy\sh());
}
hello();
?>
When I run php file.php I get the following error:
PHP Fatal error: Uncaught Error: Call to undefined function Psy\sh()
It's definitely not a PATH issue because I can access Psysh interactive shells from any terminal. Why doesn't my php script know about Psysh?
For libraries that specify autoload information, Composer generates a
vendor/autoload.php file. You can simply include this file and start
using the classes that those libraries provide without any extra work:
require __DIR__ . '/vendor/autoload.php';
Composer installs library into vendor directory and generates a file, that helps to use the code. You have to include autoload file at the top of your simple script, otherwise it won't work.
https://getcomposer.org/doc/01-basic-usage.md

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