Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the equivalent of virtual functions of c++ in PHP?

Is it abstract function xxx?

I just made a test which seems to indicate a private method to be virtual too?

class a {
 private function test()
 {
  echo 1;
 }
}

class b extends a {
 private function test()
 {
  echo 2;
 }
 public function call()
 {
  $this->test();
 }
}

$instance = new b;
$instance->call();

The output is 2

like image 651
user198729 Avatar asked Mar 21 '10 16:03

user198729


People also ask

What is virtual function in PHP?

In PHP all public and protected functions are "virtual". You can prevent functions from being overriden by prepending the final keyword. (Or by making them private, but this is probably a bad idea). In the design of the baseclass I would think of behaviors that subclasses would want to affect.

Are there virtual functions in C?

Although C doesn't provide native support for virtual functions, you can emulate virtual functions in C if you attend to all the details.

What is a virtual method C?

A virtual method is one that is declared as virtual in the base class. A method is declared as virtual by specifying the keyword "virtual" in the method signature. A virtual method may or may not have a return type. Virtual methods allow subclasses of the type to override the method.

What is virtual function in C with example?

A virtual function is a member function that you expect to be redefined in derived classes. When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the function.


1 Answers

In PHP all none private functions are virtual so there is no need to explicitly declare them as virtual.

Declaring a member function as abstract simply means that the base class cannot provide an implementation but all deriving classes should. Defining the method as abstract is the same as doing the following in C++

virtual void foo() = 0;

Which simply means that deriving classes must implement foo();

EDIT: Regarding edited question

b::call() cannot access a::test(). For this reason when calling private functions only the one in the class where it was called from will be called.

EDIT: Regarding the comment:

(From Wikipieda)

In object-oriented programming, a virtual function or virtual method is a function or method whose behaviour can be overridden within an inheriting class by a function with the same signature.

Due to the idea of explicitly stating what you pay for in C++, you have to declare functions as being virtual to allow derived classes to override a function.

class Foo{
public:
    void baz(){
        std::cout << "Foo";
    }
};
class Bar : public Foo{
public:
    void baz(){
        std::cout << "Bar";
    }
};

int main(){
    Foo* f = new Bar();
    f->baz(); //baz is not virtual in Foo, so the output is Foo
}

Change baz to be virtual

class Foo{
public:
    virtual void baz(){
        std::cout << "Foo";
    }
};
//Same Bar declaration

int main(){
    Foo* f = new Bar();
    f->baz(); //baz is virtual in Foo, so the output is Bar as it will call the derived function
}

Note, if the variable f in the above sample was of type Bar* or Bar it wouldn't matter if Foo::baz() was virtual or not as the intended type is known (The programmer explicitly supplied it)

like image 172
Yacoby Avatar answered Sep 20 '22 18:09

Yacoby