You are here: Re: classes and variable scope « PHP Programming Language « IT news, forums, messages
Re: classes and variable scope

Posted by Joe Molloy on 11/04/05 03:28

Actually, in the end, the code that worked looked like this for anyone else
in a similar situation:

<?php
$myvar = "my original string";

class myClass
{

var $mystr;

function myClass()
{
global $myvar;
$this -> mystr = $myvar;
}

function returnmyvar()
{
return "some processed text and ".$this -> mystr;
}

function usesnewstr()
{
echo $this->returnmyvar();
}
}

$myCls = &New myClass();
$myCls -> usesnewstr();
?>

Next week, inheritence ;)

Joe


"Joe Molloy" <molloy_joe@hotmail.com> wrote in message
news:Hrtaf.18148$R5.1241@news.indigo.ie...
> Hi,
>
> I'm wondering is there any way I can get a variable's value from within a
> class when the variable has been declared outside the class but in the
> same script as the class is contained in. For example, say I have the
> following script
>
> <?php
> constant myvar = "my original string";
>
> class myClass
> {
> function returnmyvar()
> {
> return "some processed text and ".$myvar;
> }
>
> function usesnewstr()
> {
> echo $this->returnmyvar();
> }
> }
>
> $myCls = &New myClass($myvar);
> $myCls -> usesnewstr();
> ?>
>
>
> Now what I'd like this to output would be:
> some processed text and my original string
> but all I get is:
> some processed text and
>
> Now I know I could make a constructor function and pass $myvar in that way
> but I would rather not have to do that as that would entail changing the
> instatiation statements whevever that class is already used. Any other
> ways to make $myvar visible to functions inside the class?
>
> Joe
>
>

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация