944,103 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1658
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 8th, 2007
0

Class question

Expand Post »
How can I use variable from the outside of the class? For example:

PHP Syntax (Toggle Plain Text)
  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
Similar Threads
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jul 8th, 2007
0

Re: Class question

Hi. You can use :

php Syntax (Toggle Plain Text)
  1. function printme() {
  2.  
  3. global $outside;
  4. print $outside; }

- Mitko Kostov
Last edited by MitkOK; Jul 8th, 2007 at 7:37 am.
Reputation Points: 59
Solved Threads: 12
Junior Poster
MitkOK is offline Offline
142 posts
since Jul 2007
Jul 8th, 2007
0

Re: Class question

Oh thanks you
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jul 8th, 2007
0

Re: Class question

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..
PHP Syntax (Toggle Plain Text)
  1. $variable = "hello";
  2.  
  3. function printme($variable) {
  4.  
  5. print $variable;
  6. }

Any questions. just ask.
Cheers.
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
Jul 8th, 2007
0

Re: Class question

Another question:

PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jul 8th, 2007
0

Re: Class question

What happens when you print it ?
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007
Jul 8th, 2007
0

Re: Class question

Probally, nothing is printed. EMPTY
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jul 8th, 2007
1

Re: Class question

You should use
php Syntax (Toggle Plain Text)
  1. $this->msg = $new_msg;
instead of
php Syntax (Toggle Plain Text)
  1. $msg = $new_msg;

- Mitko Kostov
Last edited by MitkOK; Jul 8th, 2007 at 12:14 pm.
Reputation Points: 59
Solved Threads: 12
Junior Poster
MitkOK is offline Offline
142 posts
since Jul 2007
Jul 8th, 2007
0

Re: Class question

Worked!!! Thanks you... sorry to ask many question...
But I am really new at that CLASS system in PHP thanks
ago.
Reputation Points: 350
Solved Threads: 63
Posting Pro
invisal is offline Offline
562 posts
since Mar 2005
Jul 8th, 2007
0

Re: Class question

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();
Reputation Points: 35
Solved Threads: 5
Junior Poster
dr4g is offline Offline
136 posts
since Apr 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: parse error, unexpected T_STRING in
Next Thread in PHP Forum Timeline: word to html





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC