symfony2:试图从命名空间加载类“Memcache”

Posted

技术标签:

【中文标题】symfony2:试图从命名空间加载类“Memcache”【英文标题】:symfony2: Attempted to load class "Memcache" from namespace 【发布时间】:2017-03-26 22:39:59 【问题描述】:

我使用 memcache 和 symfony2 和 ratchet 我使用这个命令行安装了 memcache

sudo apt-get install php-memcache -y

我在php.ini 文件上编辑了这些行

session.save_handler=memcache
session.save_path="localhost:11211"

但是当我使用这条线时

$memcache = new Memcache;

我收到这个错误

symfony2: Attempted to load class "Memcache" from namespace 

我的代码socketcommand.php

<?php
namespace check\roomsBundle\Command;
use Ratchet\Session\SessionProvider;
use Symfony\Component\HttpFoundation\Session\Storage\Handler;
use Ratchet\App;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

// Include ratchet libs
use Ratchet\Server\ioserver;
use Ratchet\Http\HttpServer;
use Ratchet\WebSocket\WsServer;
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
// Change the namespace according to your bundle
use check\roomsBundle\Sockets\Chat;
use Doctrine\ORM\EntityManager;
use Symfony\Component\HttpFoundation\Session\Storage\Handler\MemcacheSessionHandler;
use Symfony\Component\HttpFoundation\Session\Storage\NativeSessionStorage;

class SocketCommand extends ContainerAwareCommand

    protected function configure()
    
        $this->setName('sockets:start-chat')
            // the short description shown while running "php bin/console list"
            ->setHelp("Starts the chat socket demo")
            // the full command description shown when running the command with
            ->setDescription('Starts the chat socket demo')
        ;
    

    protected function execute(InputInterface $input, OutputInterface $output)
    
        $output->writeln([
            'Chat socket',// A line
            '============',// Another line
            'Starting chat, open your browser.',// Empty line
        ]);
        $memcache = new Memcache;
        $memcache->connect('localhost', 11211);

        $session = new SessionProvider(
            new Chat(new MyApp(), $this->getContainer()),
            new Handler\MemcacheSessionHandler($memcache)
        );

        $server = new App('localhost');
        $server->route('/sessDemo', $session);
        $server->run();
    


我的聊天.php

<?php
namespace check\roomsBundle\Sockets;
use tuto\testBundle\Entity\Users;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Ratchet\WebSocket\WsServerInterface;
use Ratchet\MessageComponentInterface;
use Ratchet\ConnectionInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Doctrine\ORM\EntityManager;
use Symfony\Component\DependencyInjection\ContainerInterface as Container;
use Symfony\Component\HttpFoundation\Session\Session;


class Chat extends Controller implements MessageComponentInterface, WsServerInterface

    protected $container;
    protected $clients;

    //protected $em;

    //protected $db;
    public function __construct(ContainerInterface $container) 
        $this->clients = new \SplObjectStorage;
        $this->container = $container;


        //$this->em = $em;
        //$this->container = $container;
        //$this->em = $em;
    

    public function onOpen(ConnectionInterface $conn) 
        $this->clients->attach($conn);

        if (!isset ($conn->Session) || !$conn->Session instanceof Session) 
            throw new \RuntimeException('Session is not defined. Make sure that SessionProvider is executed before FOSUserProvider.');
        
        try 
            $token      = unserialize($conn->Session->get('_security_main'));
            $user       = $token->getUser();
            $provider   = $this->_container->get('fos_user.user_provider.username');
            $conn->User = $provider->refreshUser($user);
         catch (Exception $ex) 
            $conn->User = null;
        
    

【问题讨论】:

尝试'$memcache = new \Memcache;'尾随反斜杠 @FrankB 没用 @FrankB 我重新启动了我的服务器,然后我再次尝试了你的代码,结果成功了,谢谢 【参考方案1】:

替换这一行$memcache = new Memcache;

用这个$memcache = new \Memcache;

【讨论】:

以上是关于symfony2:试图从命名空间加载类“Memcache”的主要内容,如果未能解决你的问题,请参考以下文章

PHP Symfony 试图从命名空间“Sonata\IntlBundle”加载类“SonataIntlBundle”[重复]

未捕获的 Symfony\Component\Debug\Exception\ClassNotFoundException:试图从命名空间“UserBundle\UserBundle”加载类“Use

使用 Symfony 1.4 的自动加载器加载命名空间类?

Symfony2 - 在链配置的命名空间中找不到类“X”

PHPUnit、接口和命名空间 (Symfony2)

尝试从命名空间加载类您是不是忘记了另一个命名空间的“使用”语句?