php using name space library not found - php

Follow the instruction to use composer to install dukpt-php, write a simple code:
<?php
use DUKPT\DerivedKey;
use DUKPT\KeySerialNumber;
use DUKPT\Utility;
$ksnObj = new KeySerialNumber($ksn);
$decryptionKey = DerivedKey::calculateDataEncryptionRequestKey($ksnObj, $bdk);
But get error:
Class 'DUKPT\KeySerialNumber' not found

You need to include composer's autoloader:
require __DIR__ . '/vendor/autoload.php';
You can read the documentation here.

I've seen some inconsistencies (really!?!) in PHP with this type of statement where you may need to use a rooted namespace 'use' statement:
use \DUKPT\Utility.

Related

Using a composer script with a hyphen in the name?

I a'm trying to use the following script from Github: https://github.com/php-webdriver/php-webdriver
Installing with composer in "/mnt/hgfs/" was easy, but loading the class in a php file seems impossible
As you can see, there is a hyphen in the name, and i can't seem to load the class in any way. I have googled for a lot and tried many things, but same problem, either i get:
Trying to use the hyphen in namespace and use i get
PHP Parse error: syntax error, unexpected '-', expecting '{' in
/mnt/hgfs/test.php on line 3
Replacing hyphen with underscore, or just removing it i get:
PHP Fatal error: Uncaught Error: Class
'php_webdriver\WebDriver\Remote\DesiredCapabilities' not found in
/mnt/hgfs/test.php:10
This is how my code looks (/mnt/hgfs/test.php):
namespace php_webdriver\WebDriver;
require 'vendor/autoload.php';
use php_webdriver\WebDriver\Chrome\ChromeOptions;
use php_webdriver\WebDriver\Chrome\ChromeDriver;
use php_webdriver\WebDriver\Remote\DesiredCapabilities;
use php_webdriver\WebDriver\Remote\RemoteWebDriver;
$host = 'http://localhost:4444/wd/hub'; // this is the default
$capabilities = DesiredCapabilities::htmlUnitWithJS();
{
$options = new ChromeOptions();
$options->addArguments(array(
'--disable-extensions',
'--no-sandbox',
'--headless',
'--no-proxy-server'
));
$capabilities = DesiredCapabilities::chrome();
$capabilities->setCapability(ChromeOptions::CAPABILITY, $options);
$capabilities->setPlatform("Linux");
}
$driver_spec = RemoteWebDriver::create($host, $capabilities, 600000, 600000);
How should I load this class?
There are a couple of things wrong here:
namespace php_webdriver\WebDriver;
You shouldn't be trying to add your code to the webdriver namespace. For a test script you don't need your own namespace. You can probably delete this line.
As for:
require 'vendor/autoload.php';
use php_webdriver\WebDriver\Chrome\ChromeOptions;
use php_webdriver\WebDriver\Chrome\ChromeDriver;
use php_webdriver\WebDriver\Remote\DesiredCapabilities;
use php_webdriver\WebDriver\Remote\RemoteWebDriver;
I get the impression you're not 100% familiar with how PSR-4 / autoloading works. The namespace is mapped to a code directory by autoload.php, and the two don't necessarily have to have the same naming structure.
Take a look at the composer.json in the webdriver project, and pay attention to the PSR-4 section.
"Facebook\\WebDriver\\": "lib/" tells you that anything in the lib directory is to be considered as being in the Facebook\WebDriver namespace.
Try
require 'vendor/autoload.php';
use Facebook\WebDriver\Chrome\ChromeOptions;
use Facebook\WebDriver\Chrome\ChromeDriver;
use Facebook\WebDriver\Remote\DesiredCapabilities;
use Facebook\WebDriver\Remote\RemoteWebDriver;

Can't create 'new Class' even after including it

I'm using the googleAds API, but when I include a class that I need in my function
it still shows me an error :
Error: Class 'TargetingIdeaSelector' not found
File: C:\wamp64\www\projet\app\Model\Keyword.php
Line: 26
I'm including it like this :
include 'C:\wamp64\www\projet\vendors\googleads\googleads_php_lib\src\Google\AdsApi\AdWords\v201710\o\TargetingIdeaSelector.php';
And further in my function, I'm using this :
// Create selector.
$selector = new TargetingIdeaSelector();
Bringing me the error above.
Is there something else to do to use a class previously included ?
Got a fix from someone better than me :
require_once __DIR__ . '../../Vendor/autoload.php';
use Google\AdsApi\AdWords\v201710\o\TargetingIdeaSelector;
use Google\AdsApi\AdWords\v201710\o\LanguageSearchParameter;
use Google\AdsApi\AdWords\v201710\o\RelatedToQuerySearchParameter;
use Google\AdsApi\AdWords\v201710\cm\Language;
use Google\AdsApi\AdWords\v201710\cm\Paging;
It was all about using the good namespaces, I just was overloaded by the quantity of files in my project and couldn't figure out the solution out of my mess.
My bad :)

How to include / require `use` operator statements?

I dynamically generate my use statements and try to include them.
use.php (Path: /App/Test/Use/use.php)
<?php
use App\Http\Utility\GeneralUtility;
use App\Models\Project;
use App\Http\Selenium;
?>
PlayController.php (Path: /App/Http/Controllers/PlayController.php)
<?php
namespace App\Http\Controllers;
require app_path() . '\Projects\Test\Use\use.php';
...
However, If I try to use a included/required class in my controller then I get the info that some classes are missing. E.g.
FatalThrowableError in PlayController.php line 30:
Class 'App\Http\Controllers\Selenium' not found
Of course it works If I write them manually into the Controller without using require:
<?php
namespace App\Http\Controllers;
use App\Http\Utility\GeneralUtility;
use App\Models\Project;
use App\Http\Selenium;
...
So why does it not work if I include / require them?
It is not possible what you are trying to do.
Here is the documentation from the PHP manual on it:
Note: Importing rules are per file basis, meaning included files will NOT inherit the parent file's importing rules.
The source comes from here and the full documentation link is here.
Use is done at compile time and include at runtime, so that makes it impossible.
But what you could do is, use something like PHP-Parser to replace the includes before the script is being executed.

PHP how to import class with namespace

I have never used namespaces in PHP before and I cannot import some library classes that use them. I'm trying to use the wsdl2php library, here is the example code on how to use the library:
$generator = new \Wsdl2PhpGenerator\Generator();
$generator->generate(
new \Wsdl2PhpGenerator\Config(array(
'inputFile' => 'input.wsdl',
'outputDir' => '/tmp/output'
))
);
However, it doesnt matter where I put my php file that executes this code, it always throws class not found error, even if I include both classes with include or require, then the library class will throw class not found error, since it wants to use yet another library class.
Here is some directory tree for reference:
[src]
[Filter]
DefaultFilter.php
...
[Wsdl2PhpGenerator]
[Console]
Applicaton.php
...
...
Config.php
Generator.php
my_php_file_here.php
namespace declaration in Config.php: namespace Wsdl2PhpGenerator; How use is used in Config.php: use Wsdl2PhpGenerator\ConfigInterface;
From what I have seen, I have added the following to the start of my php file:
namespace Wsdl2PhpGenerator;
use Wsdl2PhpGenerator\Generator;
use Wsdl2PhpGenerator\Config;
With no success, I still get
Fatal error: Class 'Wsdl2PhpGenerator\Generator' not found in C:\kajacx\programming\PHP\EET\test\wsdl2phpgenerator-master\src\kappa.php on line 8 ($generator = new \Wsdl2PhpGenerator\Generator();)
So, how to make this work?
Just use:
require_once __DIR__ . '/vendor/autoload.php';
and composer will resolve the imports for you.
(tested with the same lib and it worked)
as as I just worked with the wsdl2php lib (cloned from github) and had the same issue, here is what I did to resolve it :
1) don't forget to launch composer install before all ...
2) Add require_once dirname( dirname(__FILE__) ) . '/vendor/autoload.php'; at the top of your php file.

PHP Namespace and includes

I am using Parse and Composer. I'm new to the new Namespacing convention in PHP. So I have the following in my index file:
<?php
require 'vendor/autoload.php';
use Parse\ParseClient;
ParseClient::initialize('XXX', 'XXX', 'XXX');
use Parse\ParseQuery;
$businessQuery = new ParseQuery("Businesses");
$businessQuery->equalTo("Active", true);
$businessQuery->limit(1);
$businessQuery->ascending("Name");
$businessResults = $businessQuery->find();
include_once 'whatever.php';
Then I have a include file, which appears to not play nicely as 'ParseQuery' fails in whatever.php. So what am I doing wrong? Please tell me it's an easy fix! lol
<?
$homeQuery = new ParseQuery("Photos");
?>
Fatal error: Class 'ParseQuery' not found in /Website/whatever.php on line 4
You must set correct autoloader in composer.json here is example. About PHP autoloading convention you can read here.

Categories