Jump to content

overloading parent class method in child class


Liquid Fire

Recommended Posts

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?

[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)?

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.