Re: [PHP] accessing one object's attribute from another



Larry Brown wrote:
Hi all, I've be developing with a structured approach for a long time
and am working at improving my site by adding some classes etc. I
however, am running into an odd thing that I can't figure out what a
reasonable search syntax would yield the desired solution. The problem
is as follows:

class a {

var $thisVar;
...
}

class b {

function hello()
{
echo $first->thisVar;
}
}

$first = new a();
$first->thisVar = "world";

$second = new b();
$second->hello();


There are a number of variables and methods that are common throughout
the user's session that I'm storing in object $first. Class b has
nothing in common with a except that it needs a couple of the variables
stored in the object that is hanging around in the session.

Is $first->thisVar the wrong way to reference that variable? How can I
get to it?

$first doesn't exist to $second - you need to make it so it knows what it is.

class b {
var $first;
function __construct($first) {
$this->first = $first;
}

function hello() {
echo $this->first->thisVar;
}
}

then you:

$first = new a();
$second = new b($first);
$second->hello();

--
Postgresql & php tutorials
http://www.designmagick.com/
.



Relevant Pages

  • Re: [PHP] accessing one objects attribute from another
    ... am running into an odd thing that I can't figure out what a ... var $thisVar; ... There are a number of variables and methods that are common throughout ... stored in the object that is hanging around in the session. ...
    (php.general)
  • Re: HTA error in Windows Vista
    ... this file can be included in master HTA frames only!!! ... var strcomputer = "." ... it will just start another HTA subapp instead of calling ... it will at least fix the session ID ...
    (microsoft.public.inetsdk.programming.scripting.vbscript)
  • Re: Why Doesnt This Work?
    ... code that is supposed to allow for me to persist session state. ... var Max = 1000; ... I set the session timeout value to "1" in the web.config file. ... a "Server Error in '/' Application - Object reference no set to an ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Solution for the ASP Session Timeout Warning Problem
    ... > // The warning window pops up 1 minute before the session expires. ... > var jsTimeOut; ... > timeOutWarning)); ... case "renew": ...
    (microsoft.public.inetserver.asp.general)
  • accessing one objects attribute from another
    ... and am working at improving my site by adding some classes etc. ... var $thisVar; ... There are a number of variables and methods that are common throughout ... stored in the object that is hanging around in the session. ...
    (php.general)