Simple PHP namespace issue - php

Trying to implement a 3rd-party-lib with PHP 5.3.9-ZS5.6.0 and the lib uses namespaces. The code where I want to use the lib doesn't. So i have:
facebook.php
Mute
(Mute is the folder of the lib)
In facebook.php I try:
$app = new \Mute\Facebook\App();
Resulting in: Fatal error: Class 'Mute\Facebook\App' not found in...
The files are all in place.
I can say:
use \Mute\Facebook\App;
without getting any error/warning... What am I missing?
When I require the "App.php" via require_once I get the next error: Fatal error: Interface 'Mute\Facebook\Bases\AccessToken' not found in [...]/Mute/Facebook/App.php
Mute/Facebook/App.php:
<?php
namespace Mute\Facebook;
use Closure;
use Exception;
use Mute\Facebook\Bases\AccessToken;
use Mute\Facebook\Bases\Batchable;
use Mute\Facebook\Bases\Configurable;
use Mute\Facebook\Bases\Requestable;
use Mute\Facebook\Bases\RequestHandler;
use Mute\Facebook\Exception\CurlException;
use Mute\Facebook\Exception\GraphAPIException;
use Mute\Facebook\Exception\HTTPException;
use Mute\Facebook\Exception\InvalidArgumentException;
use Mute\Facebook\Exception\OAuthSignatureException;
use Mute\Facebook\Util;
class App implements AccessToken, Batchable, Configurable, Requestable, RequestHandler
{ [...]

function __autoload($class_name) {
require_once str_replace('\\', '/', $class_name) . '.php';
}
I created a __autoload function to "magically" require all files I need. Totally forgot about this method >.<

Related

Using Require and then Use in a class

I am trying to create an excel file by invoking a class. This is a class that I have written and inside this class, I am trying to invoke the PHPOFFICE library that is already installed on my system. My code is as follows:
class createXls {
require 'vendor/autoload.php';
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
public function createXl($loginid, $report_index, $header, $data, $fname)
{
But I am unable to get this to work because clearly require cannot be placed outside the method. How do I go about doing this?

Laravel use vendor directory php class

Recently I learn Laravel and try to implement BingAds SDK to grab some reports to my database but failed.
I have a controller named BingAdController.php in app/http/Controllers/BingAdController.php
BingAds SDK is installed via composer, they are in vendor/microsoft/bingads/samples/V12/ReportRequests.php
BingAdController.php
namespace App\Http\Controllers;
use SoapVar;
use SoapFault;
use Exception;
use SoapClient;
use Illuminate\Http\Request;
use App\BingAd;
use App\Http\Controllers\Controller;
use Microsoft\BingAds\V12\Reporting\ReportRequestStatusType;
use Microsoft\BingAds\Auth\ServiceClient;
use Microsoft\BingAds\Auth\ServiceClientType;
use Microsoft\BingAds\Samples\V12\AuthHelper;
use Microsoft\BingAds\Samples\V12\ReportRequestLibrary;
include("/Applications/MAMP/htdocs/laravel/vendor/microsoft/bingads/samples/V12/ReportRequests.php");
class BingAdController extends Controller {
public function bingadsReporting(){
ReportRequests.php
namespace Microsoft\BingAds\Samples\V12;
// require_once __DIR__ . "/vendor/autoload.php";
require_once "/Applications/MAMP/htdocs/laravel/vendor/autoload.php";
include __DIR__ . "/AuthHelper.php";
include __DIR__ . "/ReportRequestLibrary.php";
use SoapVar;
use SoapFault;
use Exception;
use Microsoft\BingAds\V12\Reporting\ReportRequestStatusType;
use Microsoft\BingAds\Auth\ServiceClient;
use Microsoft\BingAds\Auth\ServiceClientType;
use Microsoft\BingAds\Samples\V12\AuthHelper;
use Microsoft\BingAds\Samples\V12\ReportRequestLibrary;
$GLOBALS['AuthorizationData'] = null;
$GLOBALS['Proxy'] = null;
$GLOBALS['CampaignManagementProxy'] = null;
class ReportRequests {
public $DownloadPath, $length, $folder;
Laravel keep saying the class not found...
I have been stuck in this problem for 2 days... please help
You shouldn't be manually calling include or require_once in your code - Laravel uses Composer's autoloader out of the box, so you should just be able to refer to the classes you need and it will do the rest.
First, ensure that you have installed Bing's SDK through Composer - i.e. it's added in your composer.json and it was installed through the command line tool. If you just downloaded it yourself and dropped it in the vendor directory it's not going to work.
Then you should be able to call new Microsoft\BindAds\Auth\ServiceClient or whichever class you want - Composer will know where and how to find this class for you.
If you have installed it through Composer and are still having issues you'll need to provide the exact code you're having issues with as well as the full error and stacktrace you're seeing so that we can assist debugging it.
Finally I found the solution.
Add "vendor/microsoft/bingads" into vendor/composer.json -> classmap
Don't know why BingAds didn't auto update composer.json even via composer install.

Class 'app\routing' not found

I'm currently building an application that will require a few different name spaces, however most of the application I want to put under the app namespace.
In my index.php file I declared to use the app namespace, however PHP still insists my class cannot be found like so...
<?php
define('PATH',strtok ( $_SERVER['REQUEST_URI'] , '?' ));
define("ROOT",$_SERVER['DOCUMENT_ROOT'].'/',true);
require 'vendor/autoload.php';
require 'app/class_loader.php';
use Carbon\Carbon;
use app\routing;
$routing = new routing;
$routing->router();
and inside my routing class I declare it as part of the app namespace like this
<?php
namespace app;
class routing
{
However I keep getting Fatal error: Class 'app\routing' not found in /var/www/my_app/index.php on line 15
This is my autoload file
<?php
function autoload_class_multiple_directory($class_name)
{
# List all the class directories in the array.
$array_paths = array(
'app/controllers',
'app/models',
'app/services',
);
foreach($array_paths as $path)
{
$file = $path.'/'.$class_name.'.php';
if(is_file($file))
{
include $file;
}
}
}
spl_autoload_register('autoload_class_multiple_directory');
I thought this is how namespaces worked? I want to ensure I make my application as easy to follow and code in from the get-go, could somebody please help point me in the right direction?
cheers!

how to use include_once() with namespace in laravel

I am new in laravel and importing a existing php site.
I created a controller named "List" then i need to create a object of a class, coded in a file which is been include by include_once() as shown,
<?php
namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
$INCLUDE_ROOT = 'path/to/file';
include_once($INCLUDE_ROOT . "ServiceDetails.class.php");
class Lists extends Controller
{
public function show()
{
$objServiceDetails= new ServiceDetails;
.........
........
}
}
But i am getting an error like
Fatal error: Class 'App\Http\Controllers\ServiceDetails' not found
I dont have much idea of namespace "use" and "as".
May be that's why i am not able to solve this problem.
When creating a new object it is searching class in namespace location only, but it should also look in included files, i think.
If there's a namespace in current file, PHP will try to find class in current namespace and if it won't find it, you'll get fatal error. You should open ServiceDetails.class.php class to verify if there is namespace ...; at the beginning of the file (after <?php). If not you can simple add in your Lists file after:
use App\Http\Controllers\Controller;
the following line:
use ServiceDetails
and if it is, you should copy that namespace and add the following line:
use namespaceyoucopied\ServiceDetails;
of course in namespaceyoucopied place you need to put the correct copied namespace so it could look like this:
use A\B\C\ServiceDetails;
You can also look at How to use objects from other namespaces and how to import namespaces in PHP or PHP namespaces manual
You just need to add a use statement for that class so the class in the current file can "see" it.
<?php
namespace App\Http\Controllers;
$INCLUDE_ROOT = 'path/to/file';
include_once($INCLUDE_ROOT . "ServiceDetails.class.php");
use App\Http\Controllers\Controller;
use Namespace\To\ServiceDetails;
class Lists extends Controller
{
public function show()
{
$objServiceDetails= new ServiceDetails;
.........
........
}
}
However, if you are using Laravel and doing this, then you are not using the autoloading feature to its fullest. I recommend you put this file in a namespaced directory in your application and have it follow PSR-4. Then Laravel will load this for you, and it will keep your class file looking clean.
Put the file in a path like the following: /path/to/projectRoot/app/Lib/ServiceDetails.php. Then make the file look like below so it follows PSR-4:
<?php namespace App\Http\Controllers;
use App\Http\Controllers\Controller;
use App\Lib\ServiceDetails;
class Lists extends Controller
{
public function show()
{
$objServiceDetails= new ServiceDetails;
.........
........
}
}

PHP Symfony- How to inherit multiple classes from one php file

I am using Symfony 2.4
I have a PHP file OAuth.php which has multiple classes inside that.
Code of OAuth.php is as below
<?php
namespace RT\AuthBundle\DependencyInjection\Vzaar;
use Exception;
class OAuthException extends Exception {
// pass
}
class OAuthConsumer {
//some code
}
class OAuthToken {
//some code
}
?>
I am inheriting above file in Vzaar.php file which is with in same namespace and it's code is as below
<?php
namespace RT\AuthBundle\DependencyInjection\Vzaar;
/*require_once 'OAuth.php';
require_once 'HttpRequest.php';
require_once 'AccountType.php';
require_once 'User.php';
require_once 'VideoDetails.php';
require_once 'VideoList.php';
require_once 'UploadSignature.php';*/
use RT\AuthBundle\DependencyInjection\Vzaar\OAuth;
use RT\AuthBundle\DependencyInjection\Vzaar\HttpRequest;
use RT\AuthBundle\DependencyInjection\Vzaar\AccountType;
use RT\AuthBundle\DependencyInjection\Vzaar\User;
use RT\AuthBundle\DependencyInjection\Vzaar\VideoDetails;
use RT\AuthBundle\DependencyInjection\Vzaar\VideoList;
use RT\AuthBundle\DependencyInjection\Vzaar\UploadSignature;
//use RT\AuthBundle\DependencyInjection\Vzaar\OAuth\OAuthConsumer;
Class Profile
{
public static function setAuth($_url, $_method = 'GET')
{
$consumer = new OAuthConsumer('', '');
//some code
}
}
?>
Creating new object of OAuthConsumer class throws error that
Fatal error: Class 'RT\AuthBundle\DependencyInjection\Vzaar\OAuthConsumer' not found in /var/temp/rt-web/src/RT/AuthBundle/DependencyInjection/Vzaar/Vzaar.php
1/1 ClassNotFoundException: Attempted to load class "OAuthConsumer" from namespace "RT\AuthBundle\DependencyInjection\Vzaar" in /var/temp/rt-web/src/RT/AuthBundle/DependencyInjection/Vzaar/Vzaar.php line 378. Do you need to "use" it from another namespace?
Because since 2.1 Symfony uses composer, and composer uses PSR-0/PSR-4 to autoloading the classes, you have to follow the namespace convention, so you have to create those classes in separate files and in the right directory structure, what is represented in the namespace.
EDIT
As Cerad mentioned in the comment, you can specify a class map in composer, although what is considered as a best practice with symfony projects, create every class in a separate file.

Categories