CodeIgniter Forums
simple PHP class - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: simple PHP class (/showthread.php?tid=33788)



simple PHP class - El Forum - 09-08-2010

[eluser]Ajaxian64[/eluser]
Hi,

I would like to set up a php class. Problem, is that it is not a helper and not a library.
For instance a simple class that aims to provide msg status and text about problem whenever status is ko.
It is just an example.

How can I handle this with CI ?
Where can I declare my class Mess { } ? In library folder ?
I don't know wher I have to declare of my class in order I can make a new Msg() in different parts of my CI appclication (models, controller)

I hope I am clear enough ...

Thanks


simple PHP class - El Forum - 09-08-2010

[eluser]smilie[/eluser]
Hi,

Well, you could place it in library directory and autoload it (as you will be using it a lot I believe).

From there on, you can use it everywhere. Or I have missed it totally :-)

Regards,
Smilie


simple PHP class - El Forum - 09-08-2010

[eluser]Ajaxian64[/eluser]
Thanks

But isn't there another method ? I'm not so please to do it so.
Mainly because library syntax is "huge" for this.

Consider that in a model I shall create 1 message :
Code:
$this->load->library('Msg');
if (pb 1) {$this->Msg->status = FALSE; $this->Msg->why='blablabla'; return $this->Msg; }
And in controller
Code:
$msg = $this->my_model->test(); // which returns the previous line of code.
Seems to me strange.

I juste want :
Code:
If (pb1) return new Msg('blablabla',FALSE);
and In controller
Code:
$msg = $this->my_model->test();
if ($msg->status == FALSE) ....
which it seems to me better


simple PHP class - El Forum - 09-08-2010

[eluser]smilie[/eluser]
Well, by auto loading it you will do exactly as you stated :-)
When auto loaded, you do not need:

Code:
$this->load->library('Msg');

When your library is (auto)loaded, you can use:

Code:
If (pb1) return new Msg('blablabla',FALSE);

Of course, library should have class Msg Smile)

You could even place it in core directory (libraries) so if you have multiple websites they will all use it as well (every website must autoload it in it's own config).

Hope this helps.

Regards,
Smilie


simple PHP class - El Forum - 09-08-2010

[eluser]Ajaxian64[/eluser]
Thanks smilie,
then To sum up

I put the class Msg in library folder (of my application or core)
==> then I must respect the construct form :
Code:
public function __construct($msg,$status)
    {
             $this->ci =& get_instance(); // If I need CI facilities I presume...
           $this->status  = $status;
         $this->msg      = $msg;
    }
And then I autoload this
And after I can use this syntax:
Code:
new Msg('bbll',FALSE) ;

But the syntax of Msg construct is not good and MUST be function __construct() without parameter due to the fact that entering in CI process with load->library or autoloading means no parameters in construct function.
Then new Msg(param1,param2) is, I presume, not possible with library loading...
Thanks


simple PHP class - El Forum - 09-08-2010

[eluser]zhanng[/eluser]
I am curious, but why not simply create a new folder in your application directory, and include the class file directly as you would in PHP? I am not saying that putting it into the Library directory is bad, but I am simply trying to get a feel for CI styles.


simple PHP class - El Forum - 09-08-2010

[eluser]smilie[/eluser]
Hi,

I am tired and getting down on a flew so maybe this is just 'garbage' but:

Code:
public function __construct($msg='',$status='')
    {
             $this->ci =& get_instance(); // If I need CI facilities I presume...
           $this->status  = $status;
         $this->msg      = $msg;
    }

You are correct that construct will be executed upon (auto)load. But with this above, that should take care of you problem...

Or did you mean something else?

Again, tired, sick... need beer and bed :-)

Regards,
Smilie


simple PHP class - El Forum - 09-08-2010

[eluser]smilie[/eluser]
@zhanng,

You could do that, but then you will not be able to autoload it with CI.

Regards,
Smilie


simple PHP class - El Forum - 09-08-2010

[eluser]Ajaxian64[/eluser]
Many thanks smilie
Perhaps you need beer and bed but you give me the right way of doing.

Many thanks