PHWinfo banniere

Titres
PORTAIL ANNUAIRE ARTICLES COMPARATEUR HÉBERGEURS DEVIS FORUMS RÉDUCTEUR D'URL
Précédent   PHWinfo > Autres forums > Forum Programmation & Conception > php.general > accessing one object's attribute from another
S'inscrire FAQ Membres Recherche Messages du jour Marquer les forums comme lus
accessing one object's attribute from another

Réponse
 
LinkBack Outils de la discussion
Vieux 10/09/2007, 03h06   #1
Larry Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut accessing one object's attribute from another

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?

TIA,

Larry


  Réponse avec citation
Vieux 10/09/2007, 03h34   #2
Robert Degen
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] accessing one object's attribute from another

Perhaps I misunderstood the problem, but I would simply...

On So, Sep 09, 2007 at 10:06:15 -0400, 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 {
>

private $thisVar;

public function getThisVar() {
return $this->thisVar;
}

public function setThisVar($v) {
$this->thisVar = $v;
}

> ...
> }
>
> class b {
>
> function hello()
> {



echo $first->getThisVar();


> }
> }
>
> $first = new a();


$first->setThisVar("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?
>
> TIA,
>
> Larry
>
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php


--
Und komm, Du brauchst nur einen Baum
um 1000 Streichhölzer herzustellen.
Aber Du brauchst nur einen Streichholz
um 1000 Bäume abzubrennen.
Meine Güte wer soll das denn jetzt verstehen?
Ganz egal, komm wir bleiben noch etwas länger.
Die Bedeutung zahlt ja immer der Empfänger
  Réponse avec citation
Vieux 10/09/2007, 03h45   #3
Chris
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut 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/
  Réponse avec citation
Vieux 10/09/2007, 04h14   #4
Larry Brown
Aucun Avatar
 
Messages: n/a
Hébergeur:
Par défaut Re: [PHP] accessing one object's attribute from another

On Mon, 2007-09-10 at 12:45 +1000, Chris wrote:
> 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/
>


Thanks Chris and Robert, these both should work fine and was just what I
was looking for.

Larry
  Réponse avec citation
Réponse


Outils de la discussion

Règles de messages
Vous ne pouvez pas créer de nouvelles discussions
Vous ne pouvez pas envoyer des réponses
Vous ne pouvez pas envoyer des pièces jointes
Vous ne pouvez pas modifier vos messages

Les balises BB sont activées : oui
Les smileys sont activés : oui
La balise [IMG] est activée : oui
Le code HTML peut être employé : non
Trackbacks are oui
Pingbacks are oui
Refbacks are oui


Fuseau horaire GMT +1. Il est actuellement 19h53.


Édité par : vBulletin® version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0 RC5 Tous droits réservés.
Version française #16 par l'association vBulletin francophone
PHWinfo est un site Éducation Sans Frontières
Ad Management by RedTyger
©Tous droits réservés par les parties respectives
Page generated in 0,10663 seconds with 12 queries