PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, January 29, 2022

[FIXED] How to load a library from composer?

 January 29, 2022     codeigniter, composer-php, loading     No comments   

Issue

I load a composer library suited for CodeIgniter called SteeveDroz\Asset, that I can access without problem with $asset = new SteeveDroz\Asset.

I would like to be able to load it with CodeIgniter $this->load->library('SteeveDroz\Asset'), but I get the error message

Unable to load requested class: SteeveDroz\Asset

Is it possible to achieve what I want? If yes, how?


Solution

As mentionned Alex in his comment, it is required to make an adapter library. I created an all purpose class for that:

application/libraries/ComposerAdapter.php

class ComposerAdapter
{
    private $object;

    public function __construct($object)
    {
        $this->object = $object;
    }

    public function __call($method, $args)
    {
        return call_user_func_array([$this->object, $method], $args);
    }
}

application/libraries/Asset.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

require('ComposerAdapter.php');

class Asset extends ComposerAdapter
{
    public function __construct()
    {
        parent::__construct(new SteeveDroz\Asset());
    }
}

application/config/autoload.php

// ...
$autoload['libraries'] = array('asset');
// ...


Answered By - SteeveDroz
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing