sungpeng Posted February 26, 2012 Share Posted February 26, 2012 <?php $storedvar['db_table_log']='winning'; class Event1{ function Event2(){ global $storedvar; $this->log = $storedvar['db_table_log']; echo "$this->log"; } } Event1=new Event1; Event1->Event2(); ?> Can check with you how come this coding don't work? Quote Link to comment https://forums.phpfreaks.com/topic/257804-class-and-function/ Share on other sites More sharing options...
requinix Posted February 26, 2012 Share Posted February 26, 2012 Define "doesn't work". Quote Link to comment https://forums.phpfreaks.com/topic/257804-class-and-function/#findComment-1321354 Share on other sites More sharing options...
trq Posted February 26, 2012 Share Posted February 26, 2012 There could be a few reasons why this code might not work more to the however, it is very poorly written and should not be used. You should never use the global keyword within a method within a class. It completely breaks the code encapsulation provided by classes. Quote Link to comment https://forums.phpfreaks.com/topic/257804-class-and-function/#findComment-1321356 Share on other sites More sharing options...
Anon-e-mouse Posted February 26, 2012 Share Posted February 26, 2012 <?php $storedvar['db_table_log']='winning'; class Event1{ function Event2(){ global $storedvar; $this->log = $storedvar['db_table_log']; echo "$this->log"; } } Event1=new Event1; Event1->Event2(); ?> Can check with you how come this coding don't work? As the others have said it isn't brilliantly written so you could change it to something along the lines of: <?php class MyClass{ private $iLog = ''; public function MyFunction($log){ $this->iLog = $log; return $this->iLog; } } ?> Then separately call the function: MyClass->MyFunction($log) just inputting your log file or the value you want to be stored internally. I supposed if you wanted, although I'm not quite sure why you would but you could change the $iLog to public and then just after setting it call that directly, but thats not good practice to be quite honest. Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/257804-class-and-function/#findComment-1321399 Share on other sites More sharing options...
sungpeng Posted February 26, 2012 Author Share Posted February 26, 2012 <?php class MyClass{ private $iLog = 'winning'; public function MyFunction($log){ $this->iLog = $log; return $this->iLog; } } MyClass=new MyClass; MyClass->MyFunction($log); ?> Don't work? Quote Link to comment https://forums.phpfreaks.com/topic/257804-class-and-function/#findComment-1321407 Share on other sites More sharing options...
Anon-e-mouse Posted February 26, 2012 Share Posted February 26, 2012 Well just putting it like that won't work because the function expects something to be passed to it, the $log variable? You are also trying to start a new instance of a Class without assigning it to anything..? If you try: <?php class MyClass{ private $iLog = 'winning'; public function MyFunction(){ return $this->iLog; } } $MyClass = new MyClass; echo $MyClass->MyFunction(); ?> .. It works. Because firstly you have already set the iLog variable and secondly when you call it you are doing it properly, echo'ing out the returned value from the function. My original suggestion assumed you were going to pass what you wanted to store in the class when you called the function. Equally when you attempt to create a new instance of a class to use as you have tried above you assign it so you can then use it correctly. Try taking a look at: http://www.php.net/manual/en/language.oop5.basic.php that has pretty much everything you need to get started with! Hope it helps. Quote Link to comment https://forums.phpfreaks.com/topic/257804-class-and-function/#findComment-1321411 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.