Welcome Guest, Not a member yet? Register   Sign In
Autoload my own library
#1

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!
Reply
#2

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."
Reply
#3

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',
]; 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB