Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you code a class function outside of a class in PHP?

Tags:

php

In C++, I code this way:

//foo.h    
class cBar
{
    void foobar();
}

//foo.cpp
void cBar::foobar()
{
    //Code
}

I tried to do this on PHP but the parser would complain. PHP's documentation also doesn't help. Can this be done in PHP?

like image 232
MrValdez Avatar asked Dec 18 '25 16:12

MrValdez


1 Answers

No. You need to including all your function definitions inside the class block. If defining your functions in a separate structure makes you feel better you could use an interface.

interface iBar
{
    function foobar();
}


class cBar implements iBar
{
    function foobar()
    {
        //Code
    }
}

I'd suggest just getting used to coding in a new way. It's easy to code consistantly within a single language, but I think you are fighting a loosing battle if you want to do the same across languages.

like image 126
Josh Avatar answered Dec 20 '25 05:12

Josh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!