CPP-PHP Method overriding

Hello,

I have been trying to inherit from this extension:

#include <iostream>
#include <phpcpp.h>
using namespace std;
class A  : public Php::Base
{
public:
	A(){}
	virtual ~A(){}
	virtual void __construct()
	{
		
	}
	virtual void m1()
	{		
		Php::out<<"A::m1->"<<endl;
	}
	virtual void m2()
	{
		Php::out<<"A::m2->"<<endl;
		this->m1();
	}
	virtual void m3()
	{
		Php::out<<"A::m3->"<<endl;
		this->m2();
	}	
};
extern "C" {
    PHPCPP_EXPORT void *get_module() 
    {
		static Php::Extension extension("a", "1.0");
		Php::Class<A> A("A");
		A.method("__construct", &A::__construct,Php::Public);
		A.method("m1", &A::m1,Php::Public);	
		A.method("m2", &A::m2,Php::Public);
		A.method("m3", &A::m3,Php::Public);		
		extension.add(A);
		return  extension;
    }
};

Open in new window


The thing is when I try to override m2 on php like this:

class B extends A
{
	final public function m2()
	{
		echo "B::m2()<br/>";
		parent::m1();
	}
}

Open in new window


I always get this:


A::m3()
A::m2()
A::m1()

and what I expect is:


A::m3()
B::m2()
A::m1()


I must call m3

$B= new B();
$B->m3();

Open in new window


Is there any way to achieve this using php-cpp?. I am using version 1.4
LVL 6
Ludwig DiehlSystems ArchitectAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Slick812Commented:
greetings Ludwig Diehl, ,  I tried to follow your code execution paths, and in the output you show as -
    A::m3()
    A::m2()
    A::m1()

And I am not understanding what you may be asking? ?
I just wonder were you expect the call for execution of the class B m2( ) method to happen to give you the  B::m2() output alternative?
In PHP the extends as you use -
    class B extends A
does not affect the parent methods operations, as the     A class    will still produce the output as the code in it signifies , but has no changes to methods, come up from an extended class like   class B   .

so if you want the output as -
     A::m3()
        B::m2()
     A::m1()

then you initialize the   B class    and must call the Bclass method m3() as yo do here -
      $B= new B();
      $B->m3();

??? but maybe I do not understand what programming "point" or clarity that you want in a comment here?
I am puzzled by you using the "final" on your m2() method in Bclass, as this has to do with sub class extensions of "B class"? ?
And also in PHP the term "overriding" has different context and application than in C++ or C#
0
Ludwig DiehlSystems ArchitectAuthor Commented:
Hello!, thx for replying. Well maybe I was not clear. The thing is that normal behavior of A class is:

 A::m3()
A::m2()
A::m1()

But I need to add some functionality to method  "m2" before it calls "m1". I don't want to rewrite everything, just change m2 method. If you try to do it from pure C++ it does work and it also works if you write it in PHP.
What I cannot achieve is to extend C++ class A and "override" m2 method from PHP using class B.
Please let me know if it clarifies what my problem is.
0
Slick812Commented:
OK, , This does tell me more, ,  But I have little to offer for some code operations and syntax to help. I have done very little in code work that mixes C++ code and PHP code as you describe. I would think that since you say  - "from pure C++ it does work"  and you say - "it also works if you write it in PHP",   your way to go, would be to not mix the 2 code frameworks, and use either C++  or PHP. If this is not possible for you, then from a little that I know, the C++ and PHP do not "share" or link references like -
     class B extends A
between them, as the the two are not "linked" in any way in the machine code that the two languages compile to. Sorry, I got nothin.
But there are many "options" in the C++, and there may be some include that may can reference the PHP class extention?
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

Ludwig DiehlSystems ArchitectAuthor Commented:
It is not actually mixing code because A is compiled as a library (PHP extension) using PHP-CPP (which uses Zend framework). So the result is a library completely accessible from PHP, it seems to me that class A does not realize  that class B has changed m2 functionality and that's why it keeps calling A::m2() and not B::m2().
I declared m2 as virtual so it is supposed to get "late binding"  then method B::m2() should be called, but it does not happen. I think I am missing something here but I don't know what.
0
Slick812Commented:
OK, I see a little better, , BUT I have done alot of code for PHP classes, and I do not try any of that with the "virtual" and I do not try Classes any with the "abstraction" philosophy that PHP came up with. To me they seem like they know very little about what actual system "Class" code is suppose to do. . . I worked in C# for some years, and they have a real OO syntax, but the PHP (my dumb opinion) Class syntax and implementation, is a piece together boogle attempt copy of the JAVA syntax and implementation, since PHP can not define variable type, most of the Class copy setup from JAVA seems like wasted effort to me. I can not help you about pushing the sub class method operations up in to the parent, What I would do is define a several sub classes with "extends", like
     class B extends A
     class C extends A
     class D extends A

and then use that subclass for your alternate operations, , If you do not have but one sub class, there is really no use in having a parent and subclass, just code the Class as the sub class code.
0
Ludwig DiehlSystems ArchitectAuthor Commented:
The reason of using php extension is to take advantage of C++(compiled) speed vs PHP (interpreted).
I will keep trying. If I got anything I will post it here.

Thank you anyway
0
Slick812Commented:
OK, but I beleive that PHP class definitions, are NOT interpreted each time you use that PHP script, the definitions like function and Class are built (similar to compiled) only once when the script is created, or updated, and then that pre built construct is used instead of reading and rebuilding the definitions on each page access. So your speed advantage may not be much help? ? but I have not done research on that, just my opinion.
0
sarabandeCommented:
Php::Class<A> A("A");
if you create an A object you never got a chance to get B::m2 to be called.

for a virtual call of B::m2 you always need a B object or it can't work.

Sara
0
Ludwig DiehlSystems ArchitectAuthor Commented:
so any idea?. Both languages behave as expected but deriving from such extension (C++) gives this unexpected result.
0
Ludwig DiehlSystems ArchitectAuthor Commented:
I have also tried the same with Zephir. The result is exactly the same. Still don't know if I am missing something.
0
Slick812Commented:
OK , I understand your frustration, but as I said, I do not see any reason for you needing to push up a different method process, into the parent from a subclass, I would not comment here, if other EE experts were showing you the C++ and PHP code fixes. And I have already said that it is likely that the C++ and the PHP machine computations are not linked, , to have PHP code affect the C++ code, but you seem to be set on some "Speed" optimization that may happen if you use C++ to initialize the php class. There is a technical reason for the failure , BUT, You seem sure that it should be the "Same" resultants in the combo of C++ and PHP, as just coding in PHP (without C++), but I do not see that as a forgone conclusion. I can not shed any light on you wondering - "Still don't know if I am missing something." , except that the two code frameworks work independently, as far as I have seen.

Not that this is an solver for you, but I would try and test the speed optimization, that may or may not result, from your time comsuming efforts in this mix of C++ and PHP, and see if your - "take advantage of C++(compiled) speed vs PHP (interpreted)" produces a worthwhile execution time reduction?
0
Ludwig DiehlSystems ArchitectAuthor Commented:
Thx for the response. Unfortunately, there is a huge speed gain in my tests and I need that because the time it takes the script to complete some tasks consumes most of my resources and the execution time is sometimes too much, even when the code has been optimized.
Now, the reason why I wanted to do such inheritance from PHP was because I couldn't find a way to inherit using PHP-CPP.
Luckily, I recently found an alternate way to produce php extension easily using "Zephir", which does accept inheritance, so there is no reason why I need to extend it from PHP.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Slick812Commented:
Really glad you can try some alternatives, like Zephir, and get results that you require, I have never used Zephir, so I did not consider it, Thanks for the Info, I will look at Zephir, and see if it has any advantage for me.

You may want to Close this question out, by accepting your  "Zephir" comment as the answer.
0
Slick812Commented:
I just looked at the Zephir, and some remarks about it, I had no Idea that there were developers working on that sort of thing (as a C performance gain from the PHP), , Now I see more clearly what your reasons are for taking the path you did, using C for the heavy lifting in the computations, and having the PHP link back fo r you extention.
0
Ludwig DiehlSystems ArchitectAuthor Commented:
Slick812 it is great there are groups of people who develop these kind of frameworks which make developing easier and productive.
By the way, I now switched to Zephir and believe me, so far the results are amazing!. Coding is almost exactly as php with only some smooth differences.
I found this PHP-to-C library which actually translate your PHP code to Zephir. It's built on top of zephir so it will make it even easier for you to build php extensions.
0
Ludwig DiehlSystems ArchitectAuthor Commented:
Zephir was the only choice. It let me inherite classes and do many things much easier than PHP-CPP.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.