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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

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

Post 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.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

Second one today (sorta): viewtopic.php?f=1&t=123352
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

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

Post 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:
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post 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."
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

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

Post by AbraCadaver »

Beat me too it. :)
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

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

Post 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!
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

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

Post 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?
s992
Forum Contributor
Posts: 124
Joined: Wed Oct 27, 2010 3:06 pm

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

Post 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.
Post Reply