PHP Namespace and includes - php

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.

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;

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 not finding class in required file

I am using this php wrapper for multichain's json rpc api: https://github.com/Kunstmaan/libphp-multichain in a php file.
The error I see in apache error log is:
PHP Fatal error: Class 'MultichainClient' not found in /var/www/html/new.php on line 5
I'm not sure how I should adjust my code and the wrapper looks:
My code:
<?php
require_once 'libphp-multichain/src/be/kunstmaan/multichain/MultichainClient.php';
require_once 'libphp-multichain/src/be/kunstmaan/multichain/MultichainHelper.php';
$client = new MultichainClient("http://107.170.46.124:port",{usr},{pwd});
print_r($client);
</code>
MultichainClient.php Code
The "MultichainClient" class is namespaced. You need to use the namespace as well as the class name when "calling" the class.
$client = new be\kunstmaan\multichain\MultichainClient();

php using name space library not found

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.

PHP OOP autoloading class namespaces issue

I am trying to upgrade the code of a new project I am working on to comply with PSR-0.
I am using and SPL loader class, However I may be doing something wrong, I just can't spot what the issue is.
I keep getting the following error:
Fatal error: Class 'widezike\General' not found in /nfs/c03/h04/mnt/169128/domains/widezike.com/html/beta/lib/functions.php on line 14
This is my folder structure:
index.php
-lib
config.php
init.php
spl-class-loader.php
functions.php
-widezike
-General.php
This is my functions file where it begins anything to do with the server side code:
<?php
include 'init.php';
include 'config.php';
include 'spl-class-loader.php';
$loader = new SplClassLoader('General', 'lib/widezike');
$loader->register();
use widezike\General;
//Run the output buffer
General::ob();
So this is my code for now, but I can't seem to find what's causing the fatal error...
Thanks in advance
I'm taking a bit of a guess here but I think the issue is in the constructor..
$loader = new SplClassLoader('General', 'lib/widezike');
In the code you linked to they are the namespace and the include path.
Play around with those until it works is all I can suggest.
You might try:
$loader = new SplClassLoader(null, 'lib');
Or
$load = new SplClassLoad('General', 'lib');
On the other hand I personally just use a very simple spl_autoload_register function to do my class loading for me..
spl_autoload_register(function($class){
$filename = str_replace('\\', '/', $class) . '.php';
require($filename);
});
$object = new phutureproof\common\whatever();
I would then have a file /phutureproof/common/whatever.php with the contents:
<?php
namespace phutureproof\common;
class whatever
{
}
I realised that the issue was that my name spaces were wrong, so if you have the same issue just check and make sure that your namespaces are correct!

Categories