CodeIgniter Forums
Autoload my own library - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Model-View-Controller (https://forum.codeigniter.com/forumdisplay.php?fid=10)
+--- Thread: Autoload my own library (/showthread.php?tid=87319)



Autoload my own library - jcarvalho - 04-06-2023

Hi, I made a custom library and is stored inside app/Libraries and the name is Mssql.php
it starts like this
PHP Code:
<?php 
namespace App\Libraries;
class 
Mssql
{
    /* default values */

    private $connection  null;
    private $server "";
    private $database "";
    private $user "";
    private $password "";
    private $port "";

    const ICONV_TRANSLIT "TRANSLIT";
    const ICONV_IGNORE "IGNORE";
    const WITHOUT_ICONV "";

    public function __construct()
    {
        die("Just entered in lib Mssql");
        /*$db = new Database();
        $this->server = $db->mssqlServer;
        $this->database = $db->mssqlDatabase;
        $this->user = $db->mssqlUser;
        $this->password = $db->mssqlPassword;
        $this->port = $db->mssqlPort;*/
    
I try to autoload it, but it is not working because I should see "Just entered in lib Mssql" when entering in the app
My autoload.php is like this
PHP Code:
    public $psr4 = [
        APP_NAMESPACE => APPPATH// For custom app namespace
        'Config'      => APPPATH 'Config',
        'Libraries'  => APPPATH 'Libraries',
    ];

    /**
    * -------------------------------------------------------------------
    * Class Map
    * -------------------------------------------------------------------
    * The class map provides a map of class names and their exact
    * location on the drive. Classes loaded in this manner will have
    * slightly faster performance because they will not have to be
    * searched for within one or more directories as they would if they
    * were being autoloaded through a namespace.
    *
    * Prototype:
    *  $classmap = [
    *      'MyClass'  => '/path/to/class/file.php'
    *  ];
    *
    * @var array<string, string>
    */
    public $classmap = [
        'Mssql' => APPPATH 'Libraries/Mssql.php',
    ]; 


What am I missing?
Thanks!


RE: Autoload my own library - iRedds - 04-06-2023

1. First you need to read the php manual about namespaces. So that you understand what you are doing.
2. You need to read the CI documentation on file autoloading.

Autoload.php config is not loading files.
"This file defines the namespaces and class maps so the Autoloader can find the files as needed."


RE: Autoload my own library - InsiteFX - 04-06-2023

READ:
CodeIgniter 4 User Guide - CodeIgniter4 Overview - Autoloading Files

Eample:
PHP Code:
// Namespace => Path
public $psr4 = [
    APP_NAMESPACE              => APPPATH,                                // For custom app namespace
    'Config'                    => APPPATH 'Config',                      // Config namespace
    'Myth\Auth'                => ROOTPATH 'Myth/Auth',                  // Myth/Auth namespace
    'InsiteFX\Admin'            => ROOTPATH 'InsiteFX/Admin',            // InsiteFX/Admin namespace
    'PHPMailer\\PHPMailer'      => ROOTPATH 'PHPMailer/src',              // PHPMailer namespace
];


// Classmap
public $classmap = [
    'MyClass' => '/path/to/class/file.php',
];