Page 1 of 1

$this->means->what(?);

Posted: Tue Nov 02, 2010 2:29 pm
by s992
I took a stab at CodeIgniter out of boredom(too complex for my needs, but that's not what this is about)...

I understand what $this->somefunction() does and what it means.

However, I do not understand what CI is doing with $this->output->set_header(). I understand what the function does(just using it as an example), my question is with the syntax. It looks like it is calling an object's function from within the current object, is that correct? If I were to write code like this, would it work like this?

Code: Select all

class A {
	function first() {
		echo "Trying to figure out OOP.";
	}
}

class B extends A {
	function call_first() {
		$this->A->first();
	}
}

$b = new B();
$b->call_first(); // echoes "Trying to figure out OOP."
Thanks for any light you can shed on this, I'm still trying to wrap my head entirely around OOP so any answers are welcome.

Re: $this->means->what(?);

Posted: Tue Nov 02, 2010 2:43 pm
by AbraCadaver
Second one today (sorta): viewtopic.php?f=1&t=123352

Re: $this->means->what(?);

Posted: Tue Nov 02, 2010 2:46 pm
by greyhoundcode
Your first answer doesn't work as it is - but it only required a very small modification:

Code: Select all

class A {
        function first() {
                echo "Trying to figure out OOP.";
        }
}

class B extends A {
        function call_first() {
                $this->first();
        }
}

$b = new B();
$b->call_first(); // echoes "Trying to figure out OOP."
By way of explanation, when you extend a class you inherit the methods and properties of the parent class: so Class B essentially has access to function first(), pretty much the same as if it was actually coded like this:

Code: Select all

class B {

        function first() {
                echo "Trying to figure out OOP.";
        }

        function call_first() {
                $this->first();
        }
}
Hopefully that makes sense. You might achieve something similar to your Codeigniter example like this:

Code: Select all

class P {
	public function Banana() {
		echo 'Looky here';
	}
}

class Q {
	public $parrot;
	
	public function __construct() {
		$this->parrot = new P();
	}
}

$q = new Q();
$q->parrot->Banana();
* However I see I am a bit late with my reply. I am sure AbraCadaver and Jonah's combined answer is a bit more cogent anyway :wink:

Re: $this->means->what(?);

Posted: Tue Nov 02, 2010 2:51 pm
by AbraCadaver
Just for completeness, your example is totally different, but to get that to work you would just use the this keyword as B inherits the methods in A:

Code: Select all

class A {
        function first() {
                echo "Trying to figure out OOP.";
        }
}

class B extends A {
        function call_first() {
               $this->first();
        }
}

$b = new B();
$b->call_first(); // echoes "Trying to figure out OOP."
Or to override the parent but still use it you could do:

Code: Select all

class A {
        function first() {
                echo "Trying to figure out OOP.";
        }
}

class B extends A {
        function first() {
               parent::first();
        }
}

$b = new B();
$b->first(); // echoes "Trying to figure out OOP."

Re: $this->means->what(?);

Posted: Tue Nov 02, 2010 2:54 pm
by AbraCadaver
Beat me too it. :)

Re: $this->means->what(?);

Posted: Tue Nov 02, 2010 3:10 pm
by s992
Thanks to both of you - I guess I should have searched a little bit more before posting, but I didn't know what I was searching for! I got the name of it from the other thread - "method chaining."

This question actually arose because I'm working on two classes right now for a customer invoicing application - one for retrieving customer info(class Customer) and one for displaying it(class Display) - here's the simplified version(I don't have the code in front of me right now):

Code: Select all

class Customer {
	var $name;
	var $id;

	function __construct($id) {
		$this->id = $id;
		$query = "SELECT name FROM customers WHERE id = $id";
		$result = mysql_query($query);
		$this->name = mysql_result($result,0,'name');
	}
}

class Display extends Customer {
	
	function printDetails() {
		echo $this->name;
		echo $this->id;
		// Etc..obviously formatting and stuff.
	}
}

$display = new Display();
$display->printDetails();
Display extends Customer, but Customer is constructed with ID as a parameter(so that we know which customer this is). When I instantiate Display, it's not taking a paramater, so when I call $display->printDetails() it (obviously) returns nothing.

I was hoping that "$this->something->somethingelse" would be my solution as I could instantiate Customer with an ID and then call on those variables through Display like so:

Code: Select all

$somecustomer = new customer(234);
$display = new Display();
$somecustomer->display->printDetails();
It looks like this is completely separate from what I hoped it would be, but ah well.

Thanks again!

Re: $this->means->what(?);

Posted: Thu Nov 04, 2010 3:28 pm
by greyhoundcode
Your basic idea is correct, so far as I can see. You should be able to instantiate Display with an ID parameter - I'm not sure why that is proving to be a problem for you. Consider this slightly simplified version of your code, which works fine:

Code: Select all

class Customer {

        var $name;
        var $id;

        function __construct($id) {
                $this->id   = $id;
                $this->name = 'Bert';
        }
}

class Display extends Customer {
        
        function printDetails() {
                echo $this->name . '<br />';
                echo $this->id;
                // Etc..obviously formatting and stuff.
        }
}

$display = new Display(5);
$display->printDetails();
Could the error lie elsewhere in your code and what sort of error reporting level do you have on your setup?

Re: $this->means->what(?);

Posted: Sun Nov 07, 2010 11:55 am
by s992
Thanks for the response, greyhound. My problem is that I was instantiating both display and customer and then hoping that display would pick up the values from customer.