Welcome Guest, Not a member yet? Register   Sign In
New to CI and PHP Question

Hi, I've got a commercial API (else I'd post the code) class I'm trying to use as a library (I'm guessing that would be the best way to use it inside of CI.)

It has before the class name some global variables that you have to assign a value to. I've been unable to get it to work with CI.

It also does not have a function named its self in the class. Is that required with CI?

I'm guessing I need to make a constructor that assigns the values to those variable names outside of the class. Then pass the data though the $data variable. And I'm guessing I need to make a function called the class name. If I need to do this, does anything need to happen in this function?

Any help would be appreciated

Thank you


A class doesnt need a constructor unless you want to do some initialisation when it's created as an object.

Variables outside the class definition are meant to be available to the loading script/class when the class is being include()'d in procedural scripting.

You can still define them in whichever CI class you use to load the library, but the library won't be able to see them without using the &get;_instance() function in your library. (it gives access to the CI super-object)

Thanks for the information...
Not sure I know how to do it. :-)


Search the ignited code forum or the wiki for examples of what other people have done.

That's what I've been doing for the last four days... Still at it... I'll figure it somehow or another.

When I look in the wiki I've looked at almost all the other classes that people have converted, but I don't see any where they converted a class that had global variables.

I'm guessing I could hardcode the globals in the code, but I didn't want to do that....



It is a little unclear how the globals are used in the original code you want to convert
Are they hard coded at the start of the script and then used later?
If so here is how I would do it.

CI, by default, will look to see if a config file exists for your library before loading it.
So if you create a config file for your class and then let the config array be passed to the constructor you can set all of the values any way you like.

I would also change the code to use properties of the class rather than globals

i.e. if your class is named RssReader

config/Rssreader.php (Config file name must be the same as the library file name, including the same case)
$config['xxx'] = 'something';
$config['yyy'] = 'foo';
$config['zzz'] = 2;

Then in the class
class RssReader {

  var $xxx=null;
  var $yyy=null;
  var $zzz=null;

  function RssReader($config=array()) {
    if (is_array($config) && count($config)) {
  } // RssReader()

  function initialise($config=array()) {
    if (!is_array($config)) return;
    if (!count($config)) return;

    foreach ($config as $property=>$value) {
      if (isset($this->$property)) {
        $this->$property = $value;
  } // initialise()


} // class RssReader

This will then work exactly the same as any other CI class and makes it easy to modify the config because it is in the config folder where it is expected

Thank you for the information, I did not know about the config file information, that may be the trick.


Theme © iAndrew 2016 - Forum software by © MyBB