Welcome Guest, Not a member yet? Register   Sign In
How to instantiate a class...
#1

[eluser]stormbytes[/eluser]
I've been tap dancing around instantiating classes, using $this->load->library('foo') and accessing methods & properties via $this->foo->method().

But that's getting a bit lame, especially if I need multiple instances of the class.

So out of curiosity, does loading the class enable me to create an instance in the traditional way?

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

$foo= new Foo;

Also, what's the CI-politically-correct equivalent to the conventional php include/require?
#2

[eluser]Narkboy[/eluser]
Now sure about mulitple instances of library-loaded classes, never tried it.

But - I tend to drop classes I need to use like this into Helpers. Possibly not the 'best' way, but it works - when helpers are loaded the code is included - but not run, so you can call it exactly as you would without CI.
#3

[eluser]stormbytes[/eluser]
The problem with helpers is that helpers are procedural php. That is, they are just unrelated, ungrouped functions. You can probably get away using helpers some of the time, but when you need to use a class, or if you've downloaded a class from somewhere that does something cool, you're not going to rewrite that class into a helper! Smile

So far I've managed to use:

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


And then create my classes with a simple php statement:

Code:
$bar = new Foo;

That works, for the most part. UNLESS the class you're trying to call requires an argument in its constructor function. Then you can either tap-dance around the array that CI loader allows you to pass, using:

Code:
$this->load->library('foo', array(whatever, more, stuff));

But if your constructor is expecting a variable or a series of variables, then that won't fly. I've managed to rewrite some of my constructors, bracketing the code in conditionals:

Code:
class MyClass
{
function __construct($foo = NULL)
{
    if($foo)
    {
        // do stuff
    }
}

I then create my own instance of the class using:

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

$bar = new Foo($param);

$stuff = $bar->whatever_method();

But this is a ROYAL pita. For some reason CI tries to instantiate the object/class when it loads it, rather then just well.. loading it! I'd much rather have the option to either pass a constructor argument (without it having to be an array) or series of arguments, or simply 'load' the class statically without instantiating it. I guess the latter is not possible since CI makes it possible to access your loaded classes through the CI super object $this->MyClass->myMethod(). Maybe it's a trade off? Who knows... Point is. Options would have been nice.
#4

[eluser]Narkboy[/eluser]
[quote author="stormbytes" date="1291995190"]The problem with helpers is that helpers are procedural php. That is, they are just unrelated, ungrouped functions. You can probably get away using helpers some of the time, but when you need to use a class, or if you've downloaded a class from somewhere that does something cool, you're not going to rewrite that class into a helper! Smile[/quote]

Class_helper.php
Code:
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Helper_class {

    function Helper_class() {

    }

    function add( $num1 , $num2 ) {
        return $num1 + $num2;
    }

}
// End of class_helper.php

Controller:
Code:
function helper_class() {

        $this->load->helper('class');

        $new_class = new Helper_class();
        $new_class2 = new Helper_class();

        $test = $new_class->add(4,6);
        $test2 = $new_class2->add(10,35);

        echo $test;
        echo '<br />';
        echo $test2;
    }

Output:
Code:
10
45

Yes - helpers are procedural. Unless you declare a class within the helper file. Then, the class is loaded, but not instanciated. Call it at your leisure.

Am I missing what you need?
#5

[eluser]stormbytes[/eluser]
Hahaha!! No actually quite to the contrary! You hit the nail on the head! But for the makers of CI, I mean come on guys.... Does it really make any sense to force users through such hoops for something so basic??
#6

[eluser]stormbytes[/eluser]
[dupe!]
#7

[eluser]stormbytes[/eluser]
Now if you have some neat magic trick for figuring out why I can't seem to load packages.....

http://ellislab.com/forums/viewthread/175572/
#8

[eluser]Narkboy[/eluser]
Sorry - never used packages. That said, if you don't need to code to be accessible through the CI object, just extend the helper system and drop all the classes into a single helper file. Should work - but I've not tried it.

I have been playing around with this because I need some abstract classes, which don't do well in CI... Hence, using helpers, which I agree is not perfect, but actually works quite nicely. Effectively, I use them as a place to put either random functions (classic use) or to store more complex object coding.
#9

[eluser]Phil Sturgeon[/eluser]
The only hoop you are forced through is that all constructors have to be an array. This is done because libraries accept an array of config parameters if a config file of the same name exists.

Example:

Code:
function __construct($config = array())
    {
        if (count($config) > 0)
        {
            $this->initialize($config);
        }
        else
        {
            $this->_smtp_auth = ($this->smtp_user == '' AND $this->smtp_pass == '') ? FALSE : TRUE;
            $this->_safe_mode = ((boolean)@ini_get("safe_mode") === FALSE) ? FALSE : TRUE;
        }

        log_message('debug', "Email Class Initialized");
    }

If it is not an array then right now it will not be sent to the constructor. I think a good change for CI Reactor would be for the value to be passed through anyway. Agreed?
#10

[eluser]Narkboy[/eluser]
[quote author="Phil Sturgeon" date="1292007284"]If it is not an array then right now it will not be sent to the constructor. I think a good change for CI Reactor would be for the value to be passed through anyway. Agreed?[/quote]

Agreed. It would be good to be able to use classes directly, without having to modify them to work with an array input. It generally makes CI code more aligned with non-CI code.

Where are you on using helpers to store classes as described above?




Theme © iAndrew 2016 - Forum software by © MyBB