HHVM, autoload and namespace - php

The error:
\nFatal error: Class undefined: DI in /home/runeko/www/index.php on line 7
My folder structure:
- core
- DI.php
- index.php
index.php:
<?php
set_include_path( __DIR__ );
spl_autoload_extensions('.php');
spl_autoload_register();
use \core;
$di = new DI();
//i tested too: $di = \core\DI();
DI.php
<?php
namespace \core;
class DI {
}
HHVM version:
root#akai:/var/log/hhvm# hhvm --version
HipHop VM 3.0.0-dev+2014.04.03 (rel)
Compiler: heads/master-0-g6398683df312015ebc40066dfe4b9242cc933a60
Repo schema: b4303360e484eee1f582365b9994eee150d450b0
is hhvm bugged? thanks.
The solution:
*This feature isn't implemented yet* ->
https://github.com/facebook/hhvm/issues/2308

http://docs.hhvm.com/manual/en/language.types.object.php
According to the official documentation, the object instantiation is not changed as in PHP, you need to use new keyword
use \core;
$di = new DI();

Related

php composer class not found when library is installed

I'm quite new to using composer and I am not too sure what I am doing at this point. I am currently trying to use this Nmap library I found. now once I have this library installed using this commandcomposer require willdurand/nmap
I created a index.php file with
<?php
require __DIR__ . '/vendor/autoload.php';
$hosts = Nmap::create()->scan([ 'example.com' ]);
$ports = $hosts->getOpenPorts();
echo $ports;
?>
This is what my composer.json file
{
"require": {
"willdurand/nmap": "^0.5.0"
}
}
When I run this I get PHP Fatal error: Uncaught Error: Class 'Nmap' not found in /var/www/html/nmap.php:5. I have Nmap installed on my Unix system. Any help on this issue would be great.
When you do not define a current namespace, PHP looks for any references classes in the root namespace. However, it cannot find Nmap in the root namespace because it is defined in the ´Nmap´ namespace.
You have to either add the namespace to the class defenition:
$hosts = \Nmap\Nmap::create()->scan([ 'example.com' ]);
Or, add a using statement for this class at the top of your file: (under <?php ofcourse)
use Nmap\Nmap;

doctrine odm annotations or composer autoload.php not working?

I'm trying to use Doctrine MongoDB ODM 2.0 beta on a project with the Yii2 framework, with composer version 1.8.4 and PHP 7.2, but I keep getting the error Fatal error: Uncaught Error: Call to a member function add() on boolean where the code runs $loader->add('Documents', __DIR__);
bootstrap.php file (in DIR/bootstrap.php):
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
use Doctrine\ODM\MongoDB\Configuration;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Mapping\Driver\AnnotationDriver;
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
$config = new Configuration();
$config->setProxyDir(__DIR__ . '/Proxies');
$config->setProxyNamespace('Proxies');
$config->setHydratorDir(__DIR__ . '/Hydrators');
$config->setHydratorNamespace('Hydrators');
$config->setDefaultDB('fsa');
$config->setMetadataDriverImpl(AnnotationDriver::create(__DIR__ . '/Documents'));
$dm = DocumentManager::create(null, $config);
I already tried looking at How to properly Autoload Doctrine ODM annotations? and Laravel & Couchdb-ODM - The annotation "#Doctrine\ODM\CouchDB\Mapping\Annotations\Document" does not exist, or could not be auto-loaded and a host of other threads I can't quite recall for help, but I couldn't figure out a solution.
I also tried commenting out the lines below
if ( ! file_exists($file = 'C:/path/to/vendor/autoload.php')) {
throw new RuntimeException('Install dependencies to run this script.');
}
$loader = require_once $file;
$loader->add('Documents', __DIR__);
AnnotationRegistry::registerLoader([$loader, 'loadClass']);
and ran composer dump-autoload and on command line it returned Generated autoload files containing 544 classes, but then I got the problem
[Semantical Error] The annotation "#Doctrine\ODM\MongoDB\Mapping\Annotations\Document" in class Documents\Message does not exist, or could not be auto-loaded.
So the annotations are not auto-loading, and I have no idea how to fix that.
In the model I have:
<?php
namespace Documents;
use Doctrine\ODM\MongoDB\Mapping\Annotations as ODM;
use \Doctrine\ODM\MongoDB\Mapping\Annotations\Document;
/** #ODM\Document */
class Message
{
/** #ODM\Id */
private $id;
/** #ODM\Field(type="int") */
private $sender_id;
...
I also posted a thread on github at https://github.com/doctrine/mongodb-odm/issues/1976. One commenter stated that "By default, the composer autoload file returns the autoloader in question, which seems to not be the case for you." How can I fix that? The only information I can find online is to put (inside composer.json) the lines:
"autoload": {
"psr-4": {
"Class\\": "src/"
}
},
but then what class should I be loading?
I'm very confused and being pretty new to all these tools (mongodb, yii2, etc.) doesn't help at all. I'm not sure what other information would be helpful else I would post it.
Thanks in advance.
So turns out that the problem (as was mentioned in https://github.com/doctrine/mongodb-odm/issues/1976) was that autoload.php was required twice - once in bootstrap.php and once in web/index.php (of the framework). After the require line in index.php was removed, everything worked fine.

How to create an Aura DI container?

I installed a composer, downloaded the Aura, created an index.pxp and wrote in it:
require('vendor/autoload.php');
use Aura\Di\ContainerBuilder;
$builder = new ContainerBuilder();
$di = $builder->newInstance();
$object = $di->newInstance('Vendor\Package\ClassName');
But phpStorm says:Undefined namespase DI
And i have error: Fatal error: Class 'Aura\Di\ContainerBuilder' not found in... on line 4
I do as follows: http://auraphp.com/packages/3.x/Di/getting-started.html#1-1-1
In order for the composer auto-loader to pick up \Aura\Di, the dependency needs to be managed by composer.
You can easily do this by executing
composer require aura/di
which will add the dependency to your composer.json file and register with the auto-loader.
If you have manually downloaded and installed aura/di, you can revert that.

Symfony2: class included in AppKernel can not be found on webserver

I added to my project FOSUserBundle, on localhost it's works fine. But on web server I get
Fatal error: Class 'FOS\UserBundle\FOSUserBundle' not found in
/home/zone24/domains/zone24.linuxpl.info/public_html/worldclock/app/AppKernel.php on line 22
I can't cache:clear because I get this same message.
My autoload.php
<?php
use Doctrine\Common\Annotations\AnnotationRegistry;
$loader = require __DIR__.'/../vendor/autoload.php';
// intl
if (!function_exists('intl_get_error_code')) {
require_once __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs/functions.php';
$loader->add('', __DIR__.'/../vendor/symfony/symfony/src/Symfony/Component/Locale/Resources/stubs');
}
AnnotationRegistry::registerLoader(array($loader, 'loadClass'));
return $loader;
The line from AppKernel.php who make mistake
new FOS\UserBundle\FOSUserBundle(),
Folderfriendsofsymfony in /vendor has 775 permisions
Are you using APC ? If yes, restart apache to clear its cache.
If that does not help, you can always force the autoloader to register a specific namespace with $loader->add(), but you should not have to do that. FOS works fine for me without adding that.

Loading namespaced classes with Symfony 1.4's autoloader?

How to register namespaces (with PHP 5.3) in the Symfony 1.4 for the autoloader class feature (like the Symfony 2.0)?
You can use Autoloader from Symfony2 in Symfony 1.4 framework.
1. Copy Symfony2 classloaders to vendor directory of your Symfony 1.4 sandbox project:
SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/UniversalClassLoader.php
SF_ROOT_DIR/lib/vendor/Symfony2/src/Symfony/Component/ClassLoader/ApcUniversalClassLoader.php
2. Modify your SF_ROOT_DIR/config/ProjectConfiguration.class.php file as follows:
require_once dirname(__FILE__) . '/../lib/vendor/symfony/lib/autoload/sfCoreAutoload.class.php';
require_once dirname(__FILE__) . '/../lib/autoload/sfClassLoader.class.php';
sfCoreAutoload::register();
class ProjectConfiguration extends sfProjectConfiguration {
public function setup() {
$this->namespacesClassLoader();
$this->enablePlugins('sfDoctrinePlugin');
}
public function namespacesClassLoader() {
if (extension_loaded('apc')) {
$loader = new ApcUniversalClassLoader('S2A');
} else {
$loader = new UniversalClassLoader();
}
$loader->registerNamespaces(array(
'Pohon' => __DIR__ . '/../lib/vendor/Pohon/src'));
$loader->register();
}
}
3. Register desired namespaces:
eg. I want to load class:
Pohon\Tools\String\Utils\Slugify.
Filename must be:
SF_ROOT_DIR/lib/vendor/Pohon/src/Pohon/Tools/String/Utils/Slugify.php
and registered namespace as follows:
Pohon => SF_ROOT_DIR/lib/vendor/Pohon/src
You can use Composer and it's very easy. Just install it on your machine (you probably have already since it's 2015 now) and run in your project folder:
composer init
You can then install all the packages you want with composer and include just this line in your ProjectConfiguration.class.php:
require_once __DIR__.'/../vendor/autoload.php';
Note that paths may differ if you changed the default Symfony1.4 directory structure.
Symfony uses the spl_autoload_register() function to register its own autoloader (sfAutoload).
You can register your own handler in the initialize() function of your Project/Application/Plugin. (whichever applies).
This is, for example, also what the Swift_Mailer plugin does: it registers its own autoloader when needed.

Categories