• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

php object of a class in a new class

magelus

Gawd
Joined
Oct 9, 2001
Messages
655
I want to make a class, classA, that contains an instance of another class, classB. This is how I've been doing it:

Code:
<?PHP
class B{
   function B(){ //Constructor
   }
}
class A{
   var objB;
   function A(){ //Constructor
      $objB = new B();
   }
}

However, once the control leaves the constructor the newly created B object loses scope and the variable $objB is basicly pointing at nothing. I want $objB to be pointing at an instance of B at all times, such that I can access member functions and variables of B in member funcitons of A. How do I correctly do this? Thanks!
 
you need to do
Code:
class A{
   var $objB;
   function A(){ //Constructor
      $this->objB = new B();
   }
}
from http://us4.php.net/oop (scroll down some)
Within a class definition, you do not know under which name the object will be accessible in your program: at the time the Cart class was written, it was unknown that the object will be named $cart or $another_cart later. Thus, you cannot write $cart->items within the Cart class itself. Instead, in order to be able to access it's own functions and variables from within a class, one can use the pseudo-variable $this which can be read as 'my own' or 'current object'. Thus, '$this->items[$artnr] += $num' can be read as 'add $num to the $artnr counter of my own items array' or 'add $num to the $artnr counter of the items array within the current object'.
you kinda need to read that in context though

unlike java say, you must use $this. in java you can do this.whatever inside a class method or just whatever so long as you don't have a local variable calle whatever
 
wow... why does it always need to come to this... it always take public humiliation before I figure shit out. I've dealing with all other variables in that fashion, except for that specific one. gar... thank you very much for your help! :p
 
You may want to move to php 5...

As for public humiliations, this is something you will never forget now. Quite a lesson, huh?
 
doh said:
You may want to move to php 5...

As for public humiliations, this is something you will never forget now. Quite a lesson, huh?

I'd like to but my webhost doesn't, so I think I'm going to mirgrate it to work with php 5 for compatability's sake. how ever when ever I find a decent frigin' distro (I'm taking suggestions, btw), I'm setting up current web development enviroment.

And... yeah thats usually the case, and not the first time this has happend... such is my lot in life. Oh well
 
magelus said:
how ever when ever I find a decent frigin' distro (I'm taking suggestions, btw), I'm setting up current web development enviroment.

debian

:)
 
Back
Top