954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Class question

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

<?php
   $outside = 'Hello';

   class A {
       function printme() {
           print $outside;
       }
    }

   $cls = new A();
   $cls->printme();
?>


How can I do it? please :D

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

Hi. You can use :

function printme() {

           global $outside;
           print $outside;    }


- Mitko Kostov

MitkOK
Junior Poster
142 posts since Jul 2007
Reputation Points: 59
Solved Threads: 12
 

Oh thanks you :)

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

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..

$variable = "hello";
 
function printme($variable) {
 
    print $variable;
}


Any questions. just ask.
Cheers.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

Another question:

class A{
		public $msg = '';

		public function set_msg($new_msg) {
			global $msg;
			$msg = $new_msg;
		}
	}

	$message = new A();
	$message->set_msg('Hello World');
	print $message->msg;


Why this code doesn't work as what I expect

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

What happens when you print it ?

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

Probally, nothing is printed. EMPTY :D

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

You should use

$this->msg = $new_msg;


instead of

$msg = $new_msg;


- Mitko Kostov

MitkOK
Junior Poster
142 posts since Jul 2007
Reputation Points: 59
Solved Threads: 12
 

Worked!!! Thanks you... sorry to ask many question... :D
But I am really new at that CLASS system in PHP :D thanks
ago.

invisal
Posting Pro
562 posts since Mar 2005
Reputation Points: 350
Solved Threads: 64
 

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();

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

Not a problem dude, i have nothing else to do :)
Ask away.

dr4g
Junior Poster
136 posts since Apr 2007
Reputation Points: 35
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You