Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2009
    Location
    Croatia
    Posts
    38
    Thanks
    3
    Thanked 5 Times in 5 Posts

    How to call global function from method?

    Hi, I have global function WriteLog for logging and i need to use it in my class, so i imagined something like this:

    PHP Code:
    class MyClass {
        private 
    $logger null;

        public function 
    __construct($logger) {
            
    $this->logger $logger;
        }

        private function 
    log($string) {
            
    call_user_func($this->logger$string);
        }

        public function 
    foo() {
            
    $this->log('Something');
        }
    }

    $obj = new MyClass(WriteLog);

    $obj->foo(); 
    I get message:
    Code:
    Warning: call_user_func(WriteLog): First argument is expected to be a valid callback in...
    Last edited by Renato Bebić; 01-12-2010 at 10:03 AM.
    If anything is possible, then it's possible that something is impossible.

  • #2
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,855
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    Code:
    $obj = new MyClass(WriteLog);
    I think that should be
    PHP Code:
    $obj = new MyClass('WriteLog'); 
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)

  • #3
    New Coder
    Join Date
    Sep 2009
    Location
    Croatia
    Posts
    38
    Thanks
    3
    Thanked 5 Times in 5 Posts
    Quote Originally Posted by abduraooft View Post
    Code:
    $obj = new MyClass(WriteLog);
    I think that should be
    PHP Code:
    $obj = new MyClass('WriteLog'); 
    Hm, I get same message. No difference at all.
    If anything is possible, then it's possible that something is impossible.

  • #4
    New Coder
    Join Date
    Sep 2009
    Location
    Croatia
    Posts
    38
    Thanks
    3
    Thanked 5 Times in 5 Posts
    PHP Code:
    var_dump($this->logger); 
    Returns
    Code:
    string(8) "WriteLog"
    for both
    PHP Code:
    new MyClass(WriteLog
    and
    PHP Code:
    new MyClass('WriteLog'
    If anything is possible, then it's possible that something is impossible.

  • #5
    New Coder
    Join Date
    Sep 2009
    Location
    Croatia
    Posts
    38
    Thanks
    3
    Thanked 5 Times in 5 Posts
    Looks like function identifier is always string and there is no way to pass function by reference in PHP, so call_user_func only executes functions within its scope.

    Any other ideas?
    Last edited by Renato Bebić; 01-12-2010 at 10:55 AM.
    If anything is possible, then it's possible that something is impossible.

  • #6
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Have you defined the function. This works...

    PHP Code:
    class MyClass {
        private 
    $logger null;

        public function 
    __construct($logger) {
            
    $this->logger $logger;
        }

        private function 
    log($string) {
            
    call_user_func($this->logger$string);
        }

        public function 
    foo() {
            
    $this->log('Something');
        }
    }
    function 
    WriteLog($string) {
        echo 
    'String: '.$string;
    }

    $obj = new MyClass('WriteLog');

    $obj->foo(); 
    Also you should ALWAYS encapsulate your strings in quotes. if WriteLog becomes a constant in future versions of PHP you will get a broken script
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)

  • Users who have thanked JAY6390 for this post:

    Renato Bebić (01-12-2010)

  • #7
    New Coder
    Join Date
    Sep 2009
    Location
    Croatia
    Posts
    38
    Thanks
    3
    Thanked 5 Times in 5 Posts
    Ah, i forgot to include WriteLog.php

    It works now
    If anything is possible, then it's possible that something is impossible.

  • #8
    Regular Coder
    Join Date
    Dec 2009
    Location
    UK
    Posts
    495
    Thanks
    0
    Thanked 58 Times in 58 Posts
    Cool!
    My site: JayGilford.com
    Resources:
    PHP Pagination Class | Getting all page links | Handling PHP Errors properly
    If you like a users help, show your appreciation with the rep and thanks buttons :)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •