Welcome Guest, Not a member yet? Register   Sign In
How to create a library?
#1

(This post was last modified: 12-07-2019, 11:43 AM by MatheusCastro.)

I have not found anywhere how a new library is created and did not understand the difference in services and librarys that can be created.


Code:
class Library
{ }
Reply
#2

applications->library folder create File
example Acb.php make 1st word capital or in some hostings your library will not found
then make class like this

<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class abc {
functions xyz(){}
}

you can make functions inside the class

in your controller load the library and call the function
Reply
#3

Er, The above advice is more for CI3.

For CI4, create libraries as classes inside some app folder, eg app/Libraries, and make sure that they are properly namepaced.

Eg:
<?php namespace App\Libraries\MyStuff;
class MyStuff {
}


and then load/reference it with
$mystuff = new App\Libraries\MyStuff();

and away you go.
Reply
#4

(This post was last modified: 12-08-2019, 06:40 AM by MatheusCastro.)

(12-07-2019, 09:24 PM)ciadmin Wrote: Er, The above advice is more for CI3.

For CI4, create libraries as classes inside some app folder, eg app/Libraries, and make sure that they are properly namepaced.

Eg:
<?php namespace App\Libraries\MyStuff;
class MyStuff {
}


and then load/reference it with
$mystuff = new App\Libraries\MyStuff();

and away you go.

Perfect, it works! Exemple: 

PHP Code:
<?php

namespace App\Libraries\Payment;

class 
Payment
{

    public function configName($name): string
    
{
        return $name;
    }


Another thing, it is recommended to use

PHP Code:
new \App\Libraries\Payment(); 

Or: 

PHP Code:
//This
use  App\Libraries\Payment;

class 
Home extends Controller
{
    public function index()
    {
        //This
        $payment = new Payment();

    {... } 
Reply
#5

(12-07-2019, 09:24 PM)ciadmin Wrote: Er, The above advice is more for CI3.

For CI4, create libraries as classes inside some app folder, eg app/Libraries, and make sure that they are properly namepaced.

Eg:
<?php namespace App\Libraries\MyStuff;
class MyStuff {
}


and then load/reference it with
$mystuff = new App\Libraries\MyStuff();

and away you go.

Sorry I do not understand. When trying the example, I get the following error: Class 'App\Controllers\App\Libraries\MyStuff' not found
What do I still have to do, to make it work correctly?
Reply
#6

Example CI 4 Library

PHP Code:
<?php namespace App\Libraries;

/**
 * Class ExampleLibrary
 *
 * @package App\Libraries
 */
class ExampleLibrary
{
    /**
     * Class properties go here.
     * -------------------------------------------------------------------
     * public, private, protected, static and const.
     */


    // -------------------------------------------------------------------

    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    public function __construct()
    {

    }

    // -------------------------------------------------------------------

}   // End of Name Library Class.

/**
 * -----------------------------------------------------------------------
 * Filename: ExampleLibrary.php
 * Location: ./app/Libraries/ExampleLibrary.php
 * -----------------------------------------------------------------------
 */ 

Example Controller loading the Example Library:

PHP Code:
<?php namespace App\Controllers;

use 
App\Libraries\ExampleLibrary;

/**
 * Class ExampleController
 *
 * @package App\Controllers
 */
class ExampleController extends BaseController
{
    /**
     * Class properties go here.
     * -------------------------------------------------------------------
     * public, private, protected, static and const.
     */


    // -------------------------------------------------------------------

    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    public function __construct()
    {
        parent::__construct();

    }

    // -------------------------------------------------------------------

    /**
     * index ()
     * -------------------------------------------------------------------
     *
     */
    public function index()
    {
        $newExampleLibrary = new ExampleLibrary();
    }
    
    
// --------------------------------------------------------------------

}   // End of ExampleController Controller Class.

/**
 * -----------------------------------------------------------------------
 * Filename: ExampleController.php
 * Location: ./app/Controllers/ExampleController.php
 * -----------------------------------------------------------------------
 */ 

Try that.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

(12-15-2019, 09:33 AM)InsiteFX Wrote: Example CI 4 Library

PHP Code:
<?php namespace App\Libraries;

/**
 * Class ExampleLibrary
 *
 * @package App\Libraries
 */
class ExampleLibrary
{
    /**
     * Class properties go here.
     * -------------------------------------------------------------------
     * public, private, protected, static and const.
     */


    // -------------------------------------------------------------------

    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    public function __construct()
    {

    }

    // -------------------------------------------------------------------

}   // End of Name Library Class.

/**
 * -----------------------------------------------------------------------
 * Filename: ExampleLibrary.php
 * Location: ./app/Libraries/ExampleLibrary.php
 * -----------------------------------------------------------------------
 */ 

Example Controller loading the Example Library:

PHP Code:
<?php namespace App\Controllers;

use 
App\Libraries\ExampleLibrary;

/**
 * Class ExampleController
 *
 * @package App\Controllers
 */
class ExampleController extends BaseController
{
    /**
     * Class properties go here.
     * -------------------------------------------------------------------
     * public, private, protected, static and const.
     */


    // -------------------------------------------------------------------

    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    public function __construct()
    {
        parent::__construct();

    }

    // -------------------------------------------------------------------

    /**
     * index ()
     * -------------------------------------------------------------------
     *
     */
    public function index()
    {
        $newExampleLibrary = new ExampleLibrary();
    }
    
    
// --------------------------------------------------------------------

}   // End of ExampleController Controller Class.

/**
 * -----------------------------------------------------------------------
 * Filename: ExampleController.php
 * Location: ./app/Controllers/ExampleController.php
 * -----------------------------------------------------------------------
 */ 

Try that.
WORKS! Thank you very much
Reply
#8

This is a great post for beginners like me in ci4
Reply
#9

(12-15-2019, 09:33 AM)InsiteFX Wrote: Example CI 4 Library

PHP Code:
<?php namespace App\Libraries;

/**
 * Class ExampleLibrary
 *
 * @package App\Libraries
 */
class ExampleLibrary
{
    /**
     * Class properties go here.
     * -------------------------------------------------------------------
     * public, private, protected, static and const.
     */


    // -------------------------------------------------------------------

    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    public function __construct()
    {

    }

    // -------------------------------------------------------------------

}   // End of Name Library Class.

/**
 * -----------------------------------------------------------------------
 * Filename: ExampleLibrary.php
 * Location: ./app/Libraries/ExampleLibrary.php
 * -----------------------------------------------------------------------
 */ 

Example Controller loading the Example Library:

PHP Code:
<?php namespace App\Controllers;

use 
App\Libraries\ExampleLibrary;

/**
 * Class ExampleController
 *
 * @package App\Controllers
 */
class ExampleController extends BaseController
{
    /**
     * Class properties go here.
     * -------------------------------------------------------------------
     * public, private, protected, static and const.
     */


    // -------------------------------------------------------------------

    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    public function __construct()
    {
        parent::__construct();

    }

    // -------------------------------------------------------------------

    /**
     * index ()
     * -------------------------------------------------------------------
     *
     */
    public function index()
    {
        $newExampleLibrary = new ExampleLibrary();
    }
    
    
// --------------------------------------------------------------------

}   // End of ExampleController Controller Class.

/**
 * -----------------------------------------------------------------------
 * Filename: ExampleController.php
 * Location: ./app/Controllers/ExampleController.php
 * -----------------------------------------------------------------------
 */ 

Try that.

Thanks!
Reply




Theme © iAndrew 2016 - Forum software by © MyBB