| | |
Class question
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
How can I use variable from the outside of the class? For example:
How can I do it? please
PHP Syntax (Toggle Plain Text)
<?php $outside = 'Hello'; class A { function printme() { print $outside; } } $cls = new A(); $cls->printme(); ?>
How can I do it? please
Hi. You can use :
- Mitko Kostov
php Syntax (Toggle Plain Text)
function printme() { global $outside; print $outside; }
- Mitko Kostov
Last edited by MitkOK; Jul 8th, 2007 at 7:37 am.
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..
Any questions. just ask.
Cheers.
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)
$variable = "hello"; function printme($variable) { print $variable; }
Any questions. just ask.
Cheers.
GardCMS :: Open Source CMS :: Gardcms.org
Another question:
Why this code doesn't work as what I expect
PHP Syntax (Toggle Plain Text)
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
Last edited by invisal; Jul 8th, 2007 at 11:47 am.
You should use instead of
- Mitko Kostov
php Syntax (Toggle Plain Text)
$this->msg = $new_msg;
php Syntax (Toggle Plain Text)
$msg = $new_msg;
- Mitko Kostov
Last edited by MitkOK; Jul 8th, 2007 at 12:14 pm.
![]() |
Similar Threads
- Simple Class Question (C++)
- a class question (Python)
- Passing Class Objects, Instances (C++)
- Passing Class Objects, Instances (C)
- Boundary Class and Broker Class (Computer Science)
Other Threads in the PHP Forum
- Previous Thread: parse error, unexpected T_STRING in
- Next Thread: word to html
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax alerts apache api array beginner binary broken cakephp checkbox class cms code convert cron curl database date directory display download dynamic echo email error file files folder form forms function functions google hack href htaccess html htmlspecialchars image include insert integration ip java javascript joomla limit link login loop mail menu methods mlm mod_rewrite multiple mysql network object oop overwrite parse paypal pdf php problem query radio random recursion redirect regex remote script search securephp server sessions sms soap source space sql structure syntax system table tutorial update upload url validation validator variable video web xml youtube





