Jump to content

class and function


sungpeng

Recommended Posts

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.

Link to comment
Share on other sites

<?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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.