Jump to content

class and function


sungpeng

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/257804-class-and-function/#findComment-1321399
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/257804-class-and-function/#findComment-1321411
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.