Liquid Fire Posted April 13, 2007 Share Posted April 13, 2007 I have nto tried this and now that php does not support all the class(OOP) features i am used to so i though i might ask, can i overload a parent class function within the class function? Basically i have a abstract class called CFile that have some basic methods and member that every file type will need. Now i am going to have child classes(like CImageFile, CCodeFile, CTextFile, CXMLFile, etc...). In the CFile class i will have a method called UploadFileToServer(). Can i redeclare the is the CImageFile and will PHP know which one to call? Link to comment https://forums.phpfreaks.com/topic/46882-overloading-parent-class-method-in-child-class/ Share on other sites More sharing options...
gluck Posted April 13, 2007 Share Posted April 13, 2007 Not clear what you mean but I think you are looking for the key parent. if you have the same method in parent and child using parent::methodname() should do the trick. Link to comment https://forums.phpfreaks.com/topic/46882-overloading-parent-class-method-in-child-class/#findComment-228685 Share on other sites More sharing options...
Liquid Fire Posted April 13, 2007 Author Share Posted April 13, 2007 [code <?php abstract class A { public function Foo() { //do something } } class B extends A { public function Foo() { //do something } } class C extends A { } $B = new B(); $C = new C(); //will this call the B method's Foo or class A method's Foo(i would think B method's one) $B->Foo(); //this should call A's method Foo, correct me if i am wrong $C->Foo(); ?> Basically my question is can i overload class methods(functions)? Link to comment https://forums.phpfreaks.com/topic/46882-overloading-parent-class-method-in-child-class/#findComment-228764 Share on other sites More sharing options...
boo_lolly Posted April 13, 2007 Share Posted April 13, 2007 i'm not sure if you can or cannot. however, i do know that it is not good coding practice to do so. why would you want to name two different methods in two different classes the same name? btw there's a child-board here for all OOP questions. Link to comment https://forums.phpfreaks.com/topic/46882-overloading-parent-class-method-in-child-class/#findComment-228769 Share on other sites More sharing options...
Liquid Fire Posted April 13, 2007 Author Share Posted April 13, 2007 Basically the function i want to make 2 of is UploadFile. the UplaodFile in the absract Parent class is goin gto just upload the file doing no checking of anything. then in the child class, i might want to to check the file name or whatever. the function is still going to do that samething but for certain types of files i might want to do something extra. This is one compenent of OOP that i think is useful that if i have CImageFile, CPHPFile, CTextFile, CXMLFile, CZipFile and i want CImageFile, CPHPFile, CTextFile, CXMLFile to all have the same process for uploading themself then i declare that is the CFile superclass and i can declare the same method inside the CXMLFile file and do a different process but still have the name of the method the same name. Link to comment https://forums.phpfreaks.com/topic/46882-overloading-parent-class-method-in-child-class/#findComment-228792 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.