We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Access to undefined property

hi, i get the follwing error message:

<b>Notice</b>:  Access to undefined property fileService in <b>/usr/src/fileserver/app/controllers/FilesController.php</b> on line <b>74</b><br />
<br />
<b>Fatal error</b>:  Uncaught Error: Call to a member function uploadFiles() on null in /usr/src/fileserver/app/controllers/FilesController.php:74
Stack trace:
#0 [internal function]: App\Controllers\FilesController-&gt;addAction()
#1 [internal function]: Phalcon\Mvc\Micro\LazyLoader-&gt;__call('addAction', Array)
#2 [internal function]: Phalcon\Mvc\Micro\LazyLoader-&gt;callMethod('addAction', Array, NULL)
#3 /usr/src/fileserver/public/index.php(59): Phalcon\Mvc\Micro-&gt;handle()
#4 {main}
  thrown in <b>/usr/src/fileserver/app/controllers/FilesController.php</b> on line <b>74</b><br />

this is my addAction function in FileController:

public funtion addActioin()
{
    try {
            if ($this->request->hasFiles()){
                foreach ($this->request->getUploadedFiles() as $file){
                    echo $file->getName();
                    echo $file->getRealType();
                    if ($this->extensionCheck($file->getRealType())){
                        $uploads = $file;
                        //   echo FILES_PATH.$uploads->getName();
                        //   $uploads->moveTo(FILES_PATH.$uploads->getName());
                    }

                    else {
                        $errors['file'] = 'This typ of file is not supported';
                    }
                }
                $this->fileService->uploadFiles($uploads);
            }

        } catch (ServiceException $e) {
            switch ($e->getCode()) {
                case AbstractService::ERROR_ALREADY_EXISTS:
                case FilesService::ERROR_UNABLE_CREATE_FILE:
                    throw new Http422Exception($e->getMessage(), $e->getCode(), $e);
                default:
                    throw new Http500Exception(_('Internal Server Error'), $e->getCode(), $e);
            }
        }
}

and this is the uploadFiles function in FileService:

public function uploadFiles(array $uploads) {
        try {
            foreach ($uploads as $upload) {
               // $random = new Random();
               $upload->moveTo(FILES_PATH.$upload->getName());
            }
        } catch (\PDOException $e) {
            if ($e->getCode() == 23505) {
                throw new ServiceException('File already exists', self::ERROR_ALREADY_EXISTS);
            } else {
                throw new ServiceException($e->getMessage(). $e->getCode(), $e);            }
        }
    }

Can someone please tell me why i get this error? Thanks in advance

How do you register the FileService class in the DI?

Something like this should be present in your bootstrap/services file:

$di->setShared('fileService', function() {
    $fs = new FileService();
    // blabla...
    return $fs;
});


5.9k

this is how i register it in a di file:

$di->setShared('filesService', '\App\Services\FilesService');

That seems OK... (you've written filesService in plural here, but the error message matches your declaration, so i guess its ok)

Maybe you are not using the same $di instance? Try setting it explicitly on the Phalcon\Mvc\Applicationinstance with setDI().



630
Accepted
answer

The error says that it cannot find property fileService. This is because of your typo, you have declared filesService. Correct the typo.



5.9k

all of you thank you very much. i missed a s in the addAction function fileService => filesService