CodeIgniter Forums
Libraries Extending Question - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: General Help (https://forum.codeigniter.com/forumdisplay.php?fid=24)
+--- Thread: Libraries Extending Question (/showthread.php?tid=67075)



Libraries Extending Question - wolfgang1983 - 01-12-2017

I have a Parsedown.php file in my libraries folder

application  > libraries > Parsedown.php


PHP Code:
class Parsedown
{
....


And I want to extend it

application > libraries > MY_Parsedown.php

I tried require but did not make the new changes. How can I extend a library from with in application library and not system libraries.

How can I extend the parsedown into MY_Parsedown.php


PHP Code:
<?php

require APPPATH 'libraries/Parsedown.php';

class 
MY_Parsedown extends Parsedown {
    
    
protected function inlineImage($Excerpt)
    {
        $Inline parent::inlineImage($Excerpt);

        if (!isset($Inline['element']['attributes']['title'])) { return $Inline; }

        $size $Inline['element']['attributes']['title'];

        if (preg_match('/^\d+x\d+$/'$size)) {
            list($width$height) = explode('x'$size);

            $Inline['element']['attributes']['width'] = $width;
            $Inline['element']['attributes']['height'] = $height;

            unset ($Inline['element']['attributes']['title']);
        }

        return $Inline;
    }




RE: Libraries Extending Question - ciadmin - 01-12-2017

The "MY_" convention you use is the intended convention for extending CodeIgniter's core libraries. You are misusing it. See https://www.codeigniter.com/user_guide/general/core_classes.html#extending-core-class

The approach might be reasonable, with slightly different naming. You could make your class "Custom_parsedown", and start it with

Code:
require APPPATH . 'libraries/Parsedown.php';

class Custom_parsedown extends Parsedown {



RE: Libraries Extending Question - wolfgang1983 - 01-12-2017

(01-12-2017, 08:25 AM)ciadmin Wrote: The "MY_" convention you use is the intended convention for extending CodeIgniter's core libraries. You are misusing it. See https://www.codeigniter.com/user_guide/general/core_classes.html#extending-core-class

The approach might be reasonable, with slightly different naming. You could make your class "Custom_parsedown", and start it with

Code:
require APPPATH . 'libraries/Parsedown.php';

class Custom_parsedown extends Parsedown {


I made it work I have had to put the parsedown library in system library folder and  the use MY_Parsedown.php


RE: Libraries Extending Question - skunkbad - 01-12-2017

(01-12-2017, 01:19 PM)wolfgang1983 Wrote:
(01-12-2017, 08:25 AM)ciadmin Wrote: The "MY_" convention you use is the intended convention for extending CodeIgniter's core libraries. You are misusing it. See https://www.codeigniter.com/user_guide/general/core_classes.html#extending-core-class

The approach might be reasonable, with slightly different naming. You could make your class "Custom_parsedown", and start it with

Code:
require APPPATH . 'libraries/Parsedown.php';

class Custom_parsedown extends Parsedown {


I made it work I have had to put the parsedown library in system library folder and  the use MY_Parsedown.php

That would be the wrong thing to do, because if somebody upgrades CodeIgniter and doesn't know that the parsedown library needs to be saved, they'd generally replace the whole system directory and wipe it out. You should just put your extension and parsedown libraries in the application/libraries directory, and do something like this:



PHP Code:
<?php
require_once(APPPATH 'libraries/parsedown.php');
$this->load->library('custom_parsedown');
// ...
$this->custom_parsedown->whatever(); 



RE: Libraries Extending Question - ciadmin - 01-12-2017

@skunkbad ... aren't we saying the same thing?


RE: Libraries Extending Question - wolfgang1983 - 01-12-2017

(01-12-2017, 02:21 PM)ciadmin Wrote: @skunkbad ... aren't we saying the same thing?

Need to extend the library though I am going to have to put the parsedown library in system libraryes folder

This way below is not what I am after

Code:
require_once(APPPATH . 'libraries/parsedown.php');
$this->load->library('custom_parsedown');
// ...
$this->custom_parsedown->whatever();



RE: Libraries Extending Question - skunkbad - 01-12-2017

(01-12-2017, 02:21 PM)ciadmin Wrote: @skunkbad ... aren't we saying the same thing?

We are, but OP is placing the library in the system/libraries directory, so my comment was necessary to reinforce doing things the right way.


RE: Libraries Extending Question - skunkbad - 01-12-2017

(01-12-2017, 03:19 PM)wolfgang1983 Wrote:
(01-12-2017, 02:21 PM)ciadmin Wrote: @skunkbad ... aren't we saying the same thing?

Need to extend the library though I am going to have to put the parsedown library in system libraryes folder

This way below is not what I am after

Code:
require_once(APPPATH . 'libraries/parsedown.php');
$this->load->library('custom_parsedown');
// ...
$this->custom_parsedown->whatever();

If you're not after doing things the right way, then why ask for help or advice?