TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   I was just thinking (http://www.talkphp.com/advanced-php-programming/3615-i-just-thinking.html)

Tanax 11-16-2008 06:11 PM

I was just thinking
 
Hi!

I was just thinking about something.

First of all, the codesnippets in this thread will be little messy cause I'm coding them here in the WYSIWYG(or w.e it's called).

Anyways, let's say you have here:

PHP Code:

class person
{

public 
$name;



THen you have this:

PHP Code:

class participants
{

public 
$person;

public function 
getName()
{
echo 
$person->name;
}



And this controller:
PHP Code:

include('participants.php');
include(
'person.php');

$person = new person();
$participants = new participants(); 

Then you want to set a variable inside participants with an object of the person, so you can use its function inside the class

PHP Code:

$participants->person $person

Then you edit the name of the person:

PHP Code:

$person->name 'Marcus'

If I then call the getName function:

PHP Code:

$participants->getName(); 

Will it echo out the name?


As I said, messy. But anyways, my question could probably be summed up in: If you set a variable inside class b, with an instance of class a, and then change a value of class a OUTSIDE of class b(for example in a controller); Will class b get that change? Or is it like when you assign the object to a variable inside class b, it copies class a AT THAT MOMENT, and every change you make to that object will not be in the variable in class b?

Fucked up question, but whatever, you get the point.. :-P

Salathe 11-16-2008 06:56 PM

In short: yes, no.

Class instances are assigned by reference in PHP5 meaning that any changes, even after the assignment, are reflected wherever that instance is used. If you want to create a copy, use clone.

Tanax 11-16-2008 11:06 PM

Ah I see.

Thank you!


All times are GMT. The time now is 09:19 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0