Using ReflectionClass in CakePHP 4.0.4 - php

I'm trying to integrate the Playfab PHP SDK into CakePHP 4.0.4,
but Cake doesn't like the following line (included in the SDK example):
$apiReflection = new ReflectionClass("PlayFab" . $PlayFabApi . "Api");
it outputs the following error:
Class 'App\Controller\ReflectionClass' not found
As far as I know ReflectionClass is native class in PHP, so I understand CakePHP is using some kind of PHP subset that doesn't allow ReflectionClass, probably because this class allows reverse engineering and so on.
Is there anything else I am missing?
and most important, how can I make new ReflectionClass() constructor work without compromising the whole project security?

As usual: if you want to instantiate a class from the global namespace, while your current code is in another namespace, you have to prefix the class name using a backslash. Try to use :
$apiReflection = new \ReflectionClass

After some more research I've found the solution was as simple as including:
use ReflectionClass;
use ReflectionException;
use ReflectionMethod;

Related

Use Memcached in Cakephp 3

How can I access a memcached database in cakephp without using the CakePhp MemcachedEngine.php?
When I try to create a new Memcached() Object, Cake doesn't recognize that I need the php class and gives me an error like:
Class 'App\Controller\Memcached' not found
I don't want to use CakePHP's implementation of MemcachedEngine, because we had some issues with it. Is there any Memcached.php File which I can include with e.g. require_once()...?
You can try put at the top of your class: use \Memcached; or use new \Memcached().

Need help referencing PECL OAuth from a Laravel 5 app

I have a Laravel 5 application where I am building a custom OAuth interface. Everything is working well, but I cannot figure out how to reference the PECL OAuth package which is installed on the server. I'm sure its something pretty simple, but I can't figure it out and Google has oddly not been very helpful.
I have this line of code:
$oauth = new OAuth($ConsumerKey, $ConsumerSecret);
When outside of Laravel, it works perfectly, referencing the PHP package. But from within Laravel, it cannot find the class - because its not part of the application.
Can anyone help?
What's the exact error PHP is sending your way? I ask because pecl extensions make functions and classes available on a PHP level -- it shouldn't matter what userland level framework (Laravel, Symfony, etc.) you're using -- so long as the same PHP binary/web-server-extension is used, you'll have access to the pecl provided functions and classes.
My guess if you're trying to use the global class OAuth from within a namespaced file.
namespace App\Some\Namespace;
//...
$object = new OAuth;
When you do this, you're telling PHP you want the class OAuth in the same namespace. i.e., the class with the full name App\Some\Namespace\OAuth. To tell PHP you want the global level class OAuth, either refer to the class name with a namespace prefix character
$object = new \OAuth;
or import the global OAuth class with the use statement.
namespace App\Some\Namespace;
use OAuth; //confusingly, the `use` statement assumes a global namespace, no prefix needed
//...
$object = new OAuth;

include external lib in laravel?

how to include external lib in laravel ? for example twitteroauth.php.
no need oauth other packages. because i am converting Symfony code to Laravel. Thanks.
Class 'App\Controllers\Front\TwitterOAuth' not found
.
require_once base_path().'/vendor/twitter/twitteroauth.php';
$twitterOAuth = new TwitterOAuth('app_twitter_consumer_key', 'app_twitter_consumer_secret');
Likely you have a namespace problem, Laravel is looking for the class inside the 'App\Controllers\Front' namespace.
If the class is not namespaced, use
$twitterOAuth = new \TwitterOAuth('app_twitter_consumer_key', 'app_twitter_consumer_secret');
(note the backslash before the classname)
otherwise you need to refer to its namespace, something like \Twitter\TwitterOAuth or similar, but only by looking at the class file you can tell.
You could also create an alias for the class. Inside the app\config\app.php file search for the aliases array and add your class:
'Twitter' => 'TwitterOAuth' #(or whatever namespace it's in)
By the way, why not using a specific package? Have you looked on http://packagist.org to see if there's a Twitter OAuth package already adapted for Laravel? That would make things a lot easier.

PHP: forcing use of extended class in different namespace by core class / function

Background (although my problem can be generalised): I would like to integrate the Mongo core classes with PhpRbac (role based authentication) to add an additional layer of security to limit access to specific databases and collections. e.g. if the code attempts to access the collection "dbname.foo.bar.baz" it will check for existence of the PhpRbac permission "/mongo/dbname/foo/bar/baz" and ensure that the current user has permission.
I would like to force the core MongoClient::selectDB() function to return my extended version of MongoDB rather than the core MongoDB as it usually does.
e.g.
(1) Define the extended version of MongoDB
namespace MyUniqueNamespace;
class MongoDB extends \MongoDB {
public function __construct(){
die("We're definitely using the extended version!");
}
public function someStandardFunction(){
authoriseOrThrowException();
return parent::someStandardFunction();
}
}
(2) Force the core MongoClient (without modifying it) to use \MyUniqueNameSpace\MongoDB instead of MongoDB. Using use or use ... as MongoDB does not do the trick :(
use \MyUniqueNameSpace\MongoDB;
$cl = new MongoClient();
$db = $cl->selectDB("dbname"); //would die if it was using the extended function
Can anyone suggest a way of achieving this? PHP can't cast as a different class and I don't want to start rewriting the Mongo classes because this is a general problem that I'll face when integrating PhpRbac with other functionality.
Thanks for your help :)
You should add following use statement in your MongoClient class instead of where you're calling it:
use \MyUniqueNameSpace\MongoDB;

Why does Codeigniter assume I want to create an instance of the class when using $this->load?

In Codeigniter, when we use $this->load('class_name') in the controller, CI will try to create an instance of the class/model using its constructor.
But sometimes, I don't actually need an instance from that class, I just want to call some static functions from it. Also, there is a big limitation with $this->load('class_name'), it does not allow me to pass parameters to the constructor (unless we extend or modify the core class of CI).
I think the $this->load('class_name') function should only do a require_once on the class php file for me, and let me freely do things (create instance/call static functions) with the class in the controller.
Should I simply ignore this function and use require_once or writing my own __autoload function to load up the classes? This way, I just feel strange because it seems I am not writing codes inside the CI box.
You can pass parameters to your constructor. See the "Passing Parameters When Initializing Your Class" section in the user guide.
I found CodeIgniter's object creation and loading to be very limiting. I want full control over my code, and little magic in the background. I have instead started using Doctrine's Class Loader. It's very lightweight and is essentially SPL autoloading (also a good alternative). You don't need the whole Doctrine shebang with ORM and all that stuff, just the ClassLoader. There's some configuration tinkering to get this right, but it works wonders.
With PHP 5.3 I now have namespaced classes in the Application directory. For instance I created a new class in the Tests directory: Application\Tests\SomeTest.php
That test could look something like this:
namespace Tests;
class SomeTest {
...
}
I would use this class in my code (controllers, views, helpers) by simply using the fully qualified namespace (i.e. $test = new \Tests\SomeTest) or a "use" statement at the top of my code (use \Tests\SomeTest as SomeTest).
In this way I intend to replace all libraries and models with OO namespaced variants. There are many benefits to this: fast autoloading with SPL, full IDE intellisense support for classes/methods (CodeIgniter is really bad for that), your code is more portable to other frameworks or projects.
That said, I still use a lot of the CodeIgniter engine. This basically means I have $CI =& get_instance() in most of my classes. It's still a work in progress and I think the main reason I need CI is for it's database access. If I can factor that out ... and use something like Dependency Injection, then I won't need CodeIgniter in my classes at all. I will simply be using it for it's MVC framework, and using it's methods occasionally in my controllers.
I know this goes above and beyond your question, but hopefully it's some food for though - and it helps me to get it in writing too.

Categories