Class question

Reply

Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Class question

 
0
  #1
Jul 8th, 2007
How can I use variable from the outside of the class? For example:

  1. <?php
  2. $outside = 'Hello';
  3.  
  4. class A {
  5. function printme() {
  6. print $outside;
  7. }
  8. }
  9.  
  10. $cls = new A();
  11. $cls->printme();
  12. ?>

How can I do it? please
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 142
Reputation: MitkOK is an unknown quantity at this point 
Solved Threads: 12
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Class question

 
0
  #2
Jul 8th, 2007
Hi. You can use :

  1. function printme() {
  2.  
  3. global $outside;
  4. print $outside; }

- Mitko Kostov
Last edited by MitkOK; Jul 8th, 2007 at 7:37 am.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Class question

 
0
  #3
Jul 8th, 2007
Oh thanks you
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: Class question

 
0
  #4
Jul 8th, 2007
Yes.. because within a function, it has it's own scope issues, if you use the global keyword, you can access vairables/stuff outside the scope of this function.

Thus, doing a global of your $outside vairable, makes it local within your function.
However, if you change the value of $outside, it will be changed globally, not locally in the function...

Another method would be passing the $outside variable as a paramtere to the function like so..
  1. $variable = "hello";
  2.  
  3. function printme($variable) {
  4.  
  5. print $variable;
  6. }

Any questions. just ask.
Cheers.
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Class question

 
0
  #5
Jul 8th, 2007
Another question:

  1. class A{
  2. public $msg = '';
  3.  
  4. public function set_msg($new_msg) {
  5. global $msg;
  6. $msg = $new_msg;
  7. }
  8. }
  9.  
  10. $message = new A();
  11. $message->set_msg('Hello World');
  12. print $message->msg;

Why this code doesn't work as what I expect
Last edited by invisal; Jul 8th, 2007 at 11:47 am.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: Class question

 
0
  #6
Jul 8th, 2007
What happens when you print it ?
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Class question

 
0
  #7
Jul 8th, 2007
Probally, nothing is printed. EMPTY
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 142
Reputation: MitkOK is an unknown quantity at this point 
Solved Threads: 12
MitkOK's Avatar
MitkOK MitkOK is offline Offline
Junior Poster

Re: Class question

 
1
  #8
Jul 8th, 2007
You should use
  1. $this->msg = $new_msg;
instead of
  1. $msg = $new_msg;

- Mitko Kostov
Last edited by MitkOK; Jul 8th, 2007 at 12:14 pm.
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 464
Reputation: invisal is a jewel in the rough invisal is a jewel in the rough invisal is a jewel in the rough 
Solved Threads: 49
invisal's Avatar
invisal invisal is offline Offline
Posting Pro in Training

Re: Class question

 
0
  #9
Jul 8th, 2007
Worked!!! Thanks you... sorry to ask many question...
But I am really new at that CLASS system in PHP thanks
ago.
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 136
Reputation: dr4g is an unknown quantity at this point 
Solved Threads: 5
dr4g's Avatar
dr4g dr4g is offline Offline
Junior Poster

Re: Class question

 
0
  #10
Jul 8th, 2007
Yes, that's correct.

You've declared $msg as an instance variable of the class, so to refer to it you would do $this->variable.
Same if u have another function in the same class, you could call it by doing $this->function();
GardCMS :: Open Source CMS :: Gardcms.org
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC