jackpf Posted April 11, 2009 Share Posted April 11, 2009 Hi all, Just wondering if it's possible to get a variable from within a function, something like this: <?php class blah { public $blah; function blah() { $this->blah = $this->blah2()->blah2; } function blah2 { $blah2 = 'this string should appear in the public var $blah'; } } ?> But it doesn't seem to work that way - $blah remains empy. Any ideas on how to do this would be greatly appreciated, Thanks, Jack. Link to comment https://forums.phpfreaks.com/topic/153682-solved-get-a-variable-from-a-function/ Share on other sites More sharing options...
jackpf Posted April 12, 2009 Author Share Posted April 12, 2009 I guess I am asking the impossible then... Link to comment https://forums.phpfreaks.com/topic/153682-solved-get-a-variable-from-a-function/#findComment-807642 Share on other sites More sharing options...
trq Posted April 12, 2009 Share Posted April 12, 2009 Variables should be setup within a classes __construct, $blah2 is not a property of the blah2() method. Your code doesn't really make allot of sense. <?php class blah { public $blah; function __construct() { $this->blah = $this->blah2(); } function blah2 { return 'this string should appear in the public var $blah'; } } $blah = new blah; echo $blah->blah; ?> Link to comment https://forums.phpfreaks.com/topic/153682-solved-get-a-variable-from-a-function/#findComment-807644 Share on other sites More sharing options...
jackpf Posted April 12, 2009 Author Share Posted April 12, 2009 Yeah...I know that's how I should do it, but I already have a function which has an array which I'd like to extract without having to modify it. Ahh well, I guess I'll have to re-write it. Thanks anyway. Link to comment https://forums.phpfreaks.com/topic/153682-solved-get-a-variable-from-a-function/#findComment-807663 Share on other sites More sharing options...
ToonMariner Posted April 12, 2009 Share Posted April 12, 2009 why not post the code with the array in it and we'll be able to see what the deal is... Link to comment https://forums.phpfreaks.com/topic/153682-solved-get-a-variable-from-a-function/#findComment-807679 Share on other sites More sharing options...
jackpf Posted April 12, 2009 Author Share Posted April 12, 2009 I wouldn't want to post my entire code, as it's a super-cool syntax highlighter and someone might nick it Basically , a simplified version is this: <?php class code { function geshi($code) { $exist = array( 'code to be highlighted', 'more code' ); $replace = array( 'replacements', 'more replacements' ); return preg_replace($exist, $replace, $code); } } ?> And I wanted to extract the $exist and $replace arrays to use elsewhere. I took thorpe's advice and got round it by doing this, however: <?php class code { var $exist, $replace; function code() // construct { $this->exist = array( 'code to be highlighted', 'more code' ); $this->replace = array( 'replacements', 'more replacements' ); //now I can access the $exist and $replace arrays upon calling the class } function geshi($code) { return preg_replace($this->exist, $this->replace, $code); } } ?> It doesn't look as neat, as the actual code is massive, but it works better. So yeah, thanks for your help. Link to comment https://forums.phpfreaks.com/topic/153682-solved-get-a-variable-from-a-function/#findComment-807793 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.