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;
Related
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.
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.
I am following the installation instructions here to install PHP RAML Parser
I run composer install and created the index.php below but it isn't working, I get an error:
Class 'Raml\ParseConfiguration' not found in /cygdrive/c/src/myapp/Raml/Parser.php on line 83
When I hover over the line use \Raml\Parser I get the PHPStorm warning message (Alias never used)
My index.php:
<?php
require ('Raml/Parser.php');
use \Raml\Parser; // Alias \Raml\Parser is never used
$parser = new \Raml\Parser();
Can anyone suggest what I've done wrong?
Provided that the file Raml/Parser.php contains:
namespace Raml;
class Parser {}
You can either do this:
require ('Raml/Parser.php');
$parser = new \Raml\Parser();
or this:
require ('Raml/Parser.php');
use \Raml\Parser;
$parser = new Parser();
use imports a class/interface/trait into your current namespace and allows to use a shorter name instead of the fully qualified, backspaced name. It also allows to switch to a different class by only changing the use statement, and not every name reference in the whole class, but this benefit is very small because using PHPStorm brings some powerful renaming abilities itself.
I think this is very simple for many of you, but in the moment I got stuck with this. I have the following part of code:
header.php
include "facebook/autoload.php";
use Facebook\FacebookRedirectLoginHelper;
test.php
include "header.php";
$helper = new FacebookRedirectLoginHelper($redirect_url);
Why do I always get this error:
Fatal error: Class 'FacebookRedirectLoginHelper' not found in test.php on line
I thought when I include a PHP file, classes can also be used. But in this case not, why? I think I do not understand how this autoload and use works, so I would be happy for some explanation.
PHP does not inherit the namespaces nor the use statements of included/required files. This is intentional as otherwise if you include 2 files using a class aliased the same way you will get errors and you might not need all those classes in firs place.
If a class requires a namespace it has to have use statement defined with the full namespace to the particular class they need. Except in the cases where there might be aliasing. For example if you have:
// file1.php
use \My\Cool\LogWriter as Writer;
and
// file2.php
use \My\Cool\FileWriter as Writer;
Now both classes are accessible as Writer.
// test.php
require 'file1.php';
require 'file2.php';
In which case if you don't declare which class from which space you want this will give nasty error that class Writer is defined, which is true, but it is also true that the two classes are 2 separate ones.
For more information on namespaces in PHP5 see (http://php.net/manual/en/language.namespaces.php).
As a side note:
Every file, if not namespace declaration is provided is considered in the global namespace.
If a use is without leading slash the namespace might be considered relative to the current. (unsure but I think it depends on the autoloader?) (Reference here: https://stackoverflow.com/a/4879615/1747193)
I've banged my head against the wall for 3/4 of a day now and just can't see the error of my ways.
I'm creating (or trying to!) a simple package that consists of a couple of classes and 1 interface. This is in github at https://github.com/dnorth98/victoropsnotifier
Basically though, there's the following directory structure:
victoropsnotifer
src
Signiant
VictorOpsNotifier
Transport.php
VictorOpsNotifier.php
Transport is very simple:
<?php
namespace Signiant\VictorOpsNotifer;
interface Transport
{
// must POST the $message to the VictorOps REST endpoint
public function send(Messages\Message $message);
}
and the beginning of VictorOpsNotifier is
<?php
namespace Signiant\VictorOpsNotifer;
use GuzzleHttp\Client;
class VictorOpsNotifer implements Transport
{
protected $endpoint_url;
:
:
The problem comes when I try to instantiate a new object using
<?php
require_once 'vendor/autoload.php';
use Signiant\VictorOpsNotifier\Messages\CustomMessage;
use Signiant\VictorOpsNotifier\VictorOpsNotifier;
$voConfig = ['routing_key' => 'test',
'endpoint_url' => 'https://goo'];
$voHandle = new VictorOpsNotifier($voConfig);
I get back
PHP Fatal error: Interface 'Signiant\VictorOpsNotifer\Transport' not found in /tmp/djn/tests/vendor/signiant/
victoropsnotifier/src/Signiant/VictorOpsNotifier/VictorOpsNotifier.php on line 8
PHP Stack trace:
PHP 1. {main}() /tmp/djn/tests/test.php:0
PHP 2. spl_autoload_call() /tmp/djn/tests/test.php:12
PHP 3. Composer\Autoload\ClassLoader->loadClass() /tmp/djn/tests/test.php:0
PHP 4. Composer\Autoload\includeFile() /tmp/djn/tests/vendor/composer/ClassLoader.php:301
PHP 5. include() /tmp/djn/tests/vendor/composer/ClassLoader.php:412
What on EARTH am I missing? Composer is finding the package ok from my github repo and everying is in the vendor folder and looks ok. It looks like the namespaces match...so for some reason, it's just not loading the Transport.php file containing the interface.
It seems to be just a typo:
namespace Signiant\VictorOpsNotifer;
^ no `i` in here
but
use Signiant\VictorOpsNotifier\VictorOpsNotifier;
^ but here it is
same thing with the class name.
Also, you're declaring namespace Signiant as src/, but it's really src/Signiant
By the way, there's no need to declare this package as psr-0, better to use psr-4 insetad. Not a big deal, just for conformity.
P.S. The strange thing it was complaining about interface, while it shouldn't have to until it hit the class, and it certainly must not hit the class while there was a typo.
P.P.S You can easily avoid those typo errors by using a proper IDE, PHPStorm, for example. It would highlight name of the class as missing as long as there wre typos (and i don't even start talking about autocompletion).
class VictorOpsNotifer implements \Signiant\VictorOpsNotifer\Transport{}
try add namespace before your interface.