Welcome Guest, Not a member yet? Register   Sign In
object oriented class
#1

[eluser]dadamssg[/eluser]
I only know object oriented programming because i learned from using codeigniter. So i have little understanding of it outside of codeigniter.

Was wondering how you assign a class into a variable

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

class Numbersclass {

public function numbers($number_one,$number_two)
    {
        //load codeigniter object
        $CI =& get_instance();
        
        //load libraries/database/models to use
        $CI->load->library('session');
        $CI->load->helper(array('form', 'url'));
  
                $this->number_one = (int) $number_one;
                $this->number_two = (int) $number_two;

        }
public function multiply_numbers()
       {
               $number = number_one * $this->number_two;
                return $number;
       }
public function divide_numbers()
       {
               $number = $this->number_two/$this->number_one;
               return $number;
       }
public function subtract_numbers()
       {
               $number = $this->number_two - $this->number_one;
               return $number;
       }
}
?>
From what i gather, outside of codeigniter, is if you define a variable with "$this->" in front of it you can use it through the different functions of a class. I think thats right.

And then when i reference it in my controller like this

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

class Acontroller extends CI_Controller {

public function __construct()
   {
        parent::__construct();
        $this->load->helper(array('form', 'url', 'html','string'));
        $this->load->library(array('session','form_validation','numbersclass'));
        $this->load->database();
        $this->load->model(array('some_model'));
        
    }
public function index()
    {
                $number = new $this->numbersclass->numbers("5","45");

                echo $number->multiply_numbers();

                echo $numbers->divide_numbers();

                echo $numbers->subtract_numbers();

          }
}
?>

I also understand(i think) you can load all of the functions in the class when you put "new" in front of referencing the class. You load the class as an object and then can reference the different functions with "->". But this code doesn't work. I think because i don't know how to load the class as an object. I'm referencing the "numbers" function in the "numbersclass" when i should somehow be referencing the whole thing. I don't know how to do that though.

i have just been using on of functions in the class to call the other functions and then putting all of those returns into an array and then returning that single array. It'd be nice if i could reference the functions like i have in the above code.

Can someone point me in the right direction?
#2

[eluser]ted wong[/eluser]
You should do it in the library first, then, you can start load your library, in this case, your Numbersclass.
Don't expect the Codeigniter do this magic for you. Take a look:
http://ellislab.com/codeigniter/user-gui...aries.html
#3

[eluser]Rolly1971[/eluser]
the way your doing it is not needed.

for example

Code:
public function index()
    {
                $number = new $this->numbersclass->numbers("5","45");

                echo $number->multiply_numbers();

                echo $numbers->divide_numbers();

                echo $numbers->subtract_numbers();

          }

is better written like:

Code:
public function index()
    {
                $this->numbersclass->numbers("5","45");
                echo $this->numbersclass->multiply_numbers();

                echo $this->numbersclass->divide_numbers();

                echo $this->numbersclass->subtract_numbers();

          }

because the way CI works is when it loads a class using: $this->load->library('classname') a reference object is automatically created with the same name as the class.

the way you are coding it your creating a reference to a class from a reference to a class which is a bit in-efficient.




Theme © iAndrew 2016 - Forum software by © MyBB