Page 1 of 1

Polymorphic functions

Posted: Fri Nov 15, 2002 7:40 am
by jdmiller15
I'm and embedded C and C++ programmer trying to pickup a little on PHP. There is a few things that I don't see. I don't see anything about polymorphism in the online manual. I ran across an old copy of PHP3 Programming and it makes comment of polymorphic functions. I don't see anything in it that talks about virtual or pure virtual methods. If I create a base class pet and have class dog and class cat that inheriate pet, can I define an array of pointers to pet and assign dog to petarray[0] and cat to petarray[1] and then be able to call petarray[0] to get the features of dog and call petarray[1] to get the features of cat?

The other thing that confuses me is that there are constructors but no disctructors. There is a new without a delete. How do you free the memory of an instance when it is no longer needed?

Thanks

David Miller

Posted: Fri Nov 15, 2002 10:45 am
by BDKR
One thing you should know is that PHP OO model isn't as extensive as some other languages. However, the question of how extensive it needs to be is of debate considering the vast majority of it's use is centered around web developement.

For more information though, I would suggest you check out this link.
http://www.php.net/manual/language.oop.php

Now for some less than reliable ranting.
The other thing that confuses me is that there are constructors but no disctructors. There is a new without a delete. How do you free the memory of an instance when it is no longer needed?
That's handled by the PHP scripting engine. And when you get right down to it, how long do you expect this stuff to be around in a web environment? Now if you are programming something other than a web page/script or bash script, and it actually runs for some time, much like a PHP-GTK app or the load balancer I did, then this warrants some consideration. But even still, in a dynamically typed language, what's wrong with just doing...

Code: Select all

unset($your_object);
... if you want to be sure it's not hanging around getting in the way?

Memory management isn't something that isn't normally thought about in PHP.
If I create a base class pet and have class dog and class cat that inheriate pet, can I define an array of pointers to pet and assign dog to petarray[0] and cat to petarray[1] and then be able to call petarray[0] to get the features of dog and call petarray[1] to get the features of cat?
I don't see why you can't do this. However, it's a matter of knowing what PHP provides then either coding within it's documented (and community accepted) boundaries, or bastardizing what's there to reach your objective.

For instance, pointers don't really exist in PHP, nor do things like structures. However, I use structures and pointers in my programming by taking liberties with what PHP does provide. I can create a class without methods and then create functions that act upon the elements of those method-less classes. Alas, structures.

In short, you can do whatever you need in PHP, but you can't allways expect to do it the same way you did it elsewhere.

Also, being a dynamically typed language, the lack of type declaration and memory management may seem a little strange, or even crazy.

Cheers,
BDKR

Posted: Fri Nov 15, 2002 10:51 am
by BDKR
Oh, err..... :oops: I forgot.

What are ....
virtual or pure virtual methods
:?:

Perhaps you could school us a bit.

Cheers,
BDKR

Posted: Fri Nov 15, 2002 11:21 am
by jdmiller15
You asked what is virtual and pure virtual methods in C++.
In C++ a base-class pointer can be used to point to an object of any class derived from that base.
A virtual method in the base class is proceeded with the word virtual. This allows a derived class to define the same method with the same parameters. When you have a pointer that is a pointer to the base class and you assign the address of the dedrived class to the pointer and then call this method it will execute the method in the dirived class unless it doesn't exist and then it will execute the method in the base class. If the method is defined in the base as pure virtual, the base will have no code for the method and there will be a compile error if the derived class does not define the method.

This is the basses for popymorphism in C++, allowing you to have a pointer to a base class and it executes methods from the desired derived class. My example of a pet base class and derived classes of dog and cat. I can create a base class pet and derived classes of cat and dog then make two pointers to pet and assigne an instance of cat to one and an instance of dog to the other. Then if I have a method, is_tree_climber(), I would make it pure virtual in the base and return the porper response from cat or dog.

David Miller

Posted: Fri Nov 15, 2002 11:26 am
by volka
the very simple answer is: you won't find this in php 4.2 ;)
I don't see anything about polymorphism in the online manual
you can only override methods of classes not functions in global scope. They are located (like all functions are) at runtime. It doesn't matter how they were introduced to the running script, they simply must exist when the runtime env. has to perform the lookup (when your script invokes them). So at one condition you may include script A introducing a function xyz() and at another condition script B also implementing a function called xyz() but doing something else, but both can't be in the same scope at the same time.
I don't see anything in it that talks about virtual or pure virtual methods
Sure, since there is no automatic mechanism to check the hierarchically relationship. Take a look at how parameter are passed. They are all typeless.
And so are variables. They all can reference any (class-)type. If you're familiar with e.g. COM this is similiar to the type VARIANT (or ANY in CORBA or Object in Java or ... or ...). In all of these 'languages' you have several functions to determine an objects hierarchical releationship to another class/object/interface/what so ever (like RTTI in C++). And so you have in PHP. Take a look at http://www.php.net/manual/en/ref.classobj.php
.... then be able to call petarray[0] to get the features of dog and call petarray[1] to get the features of cat
So the answer is: yes ;)
How do you free the memory of an instance when it is no longer needed?
like e.g. in Java there is an automatic garbage collection. Each object (simple types like integers as well) has a reference counter. When this counter is 0 no more references are available to the script, it is no longer possible to access the object. In this state the gc can chose to free the ressources at any time (afaik this is implemented quite strict in Zend/php - more than in java where there may be weak references and so on). Since this mechanism is implemented for all types there's no need for another automatic destruction construct within the language. If you need more control over the desctruction process you have to implement it on your own (e.g. calling a finalize() function for each of your objects)

Code: Select all

$myInstance = new MyClass();
$myInstance = null;
there is no other reference to the object created by new MyClass so the ref.counter is decreased to 0 after null is assigned to $myInstance and the object's ressources may be freed.

For more advanced OO-features you may want to take a look at php 4.3 which is still prerelase but available at http://www.php.net

Posted: Fri Nov 15, 2002 12:46 pm
by BDKR
Sorry I didn't think about this earlier. faqts.com has some good information along these lines in what is basicaly a faq. The links is...

http://www.faqts.com/knowledge_base/index.phtml/fid/39

Cheers,
BDKR