Jump to content

calling Child method from parent


delickate

Recommended Posts

Hi,

I've a two classes A,B. A is a parent class and B is a child class. B is extends from A. I made an object of class A. Now i want to access function of class B from this object instance.

e.g

class A
{
    function myfunctiona()
    {
        .......
        .......
    }
}

class B extends A
{
   function chlidfunction()
   {
       return "hyne";
   }
}

$sani = new A();
//here i want to access
echo $sani->b->childfunction();

 

please guide how can called child class function from parent object instance?

Thanks

Link to comment
Share on other sites

Closest you could do would be

 

<?php
class A
{
var $b;
}
class B extends A
{
function __construct()
{
   $this->b = $this;
}
}

I honestly don't see much point, as most of the time you don't need to access child classes.

 

Another way would be

class A
{
var $b;
function __construct()
{
   $this->b = new b;
}
}
class b
{
function test() { echo 'test b'; }
}

Link to comment
Share on other sites

No, PHP doesn't support multiple inheritance. Why do you need to call a child method from a parent? If you explain what you're trying to do I'm sure we can suggest a better way of doing it.

 

Slightly off-topic, but PHP4 mrdhood?

Link to comment
Share on other sites

Thanks guys,

Actually i made some classes. one is general, one for database functions, one for images functions, one for blog, one for forum.

Now i want to join all these classes into one class. suppose if i join all classes into general class and made object of general class, i want that its instance should be able to access all functions of all classes. that is what i want. don't want to create separate objects for each class...

hope you understand my point...

Please suggest accordingly...

Thanks

Link to comment
Share on other sites

Now i want to join all these classes into one class.

 

That goes completely against any definition of good design. Objects should only inherit from each other if they are actually related. This generally means that classes in a hierarchy are of the same type.

 

suppose if i join all classes into general class and made object of general class, i want that its instance should be able to access all functions of all classes. that is what i want

 

Don't use classes then. Just continue writing procedural cod easy it is very easy to write spaghetti code (what your describing) this way.

Link to comment
Share on other sites

Now i want to join all these classes into one class. suppose if i join all classes into general class and made object of general class,

Sweet child of mine.. that is not the idea of OO programming. Objects instantiated from classes should mimic real world objects. They should be separated into their own space. If an object of type A requires the use of an object of type B then a method needs to be defined in A that can accept B as a parameter i.e

 

<?php
class A {

function do_stuff() { }

function i_need_obj_b_to_do_stuff(B $obj) {
$obj->do_some_more_stuff();
}

}

class B {

function do_some_more_stuff() { }

}

// usage
$a = new A;
$b = new B;
$a->do_stuff();
$a->i_need_obj_b_to_do_stuff($b);
?>

Link to comment
Share on other sites

Oddly 'var' was un-deprecated in 5.3, but only for backwards compatibility. Now though you still have 5 - 5.2, the most widely used versions, where it will generate a deprecated warning. To me it seems a little pointless to change their minds now, but either way you should still use the visibility keywords instead.

Link to comment
Share on other sites

Hi and thanks a lot of  all friends and my uncle programmer...

I don't think that you guys got my point.... Let me explain it in some easy way. (Just remind the concept of tree structure)

my class hierarchy is like this:

                                                                                                                      A

                                                                                                                    /    \

                                                                                                                  /      \

                                                                                                                  B        C

                                                                                                                / \        /

                                                                                                                D  E    F

 

If you see this diagram you'll find that every class is related to other class in some way.  so what i want is... Create a single object whose instance should be able to access all other classes. e.g

if i made and object of class D. so its instance not only get the functions if its own but all of others like B,A, F etc...........

Hope you've got my point now.

Please advise how will it possible...

Thanks

Link to comment
Share on other sites

As Thorpe has said your design is bad. From your post

 

Actually i made some classes. one is general, one for database functions, one for images functions, one for blog, one for forum.

 

I a real world scenario, a database has nothing to do with an image, an image has nothing to do with a forum (forum is a bit general, this should be split into component parts), and a forum has nothing to do with a database or a blog (again a blog is a bit general).

 

Therefore, as classes they should not be inheriting each others methods. As I have stated i a previous post, if an object requires the use of another, there should be a method defined that can accept the object it requires. For instance, in your blog website there may be a function where a user could upload an image into their blog. In your blog class(es) there should be a method that deals with this functionality. Now this function will  need to use a method within the image class to upload and manipulate the image. The blog class should NOT inherit the image class! A method in the blog class should accept the image object as a variable.

 

The main point of OO programming is to separate all the components. This is called loose coupling. If everything is tied together it is tightly coupled meaning you wouldn't be able to take part of the structure and use it in another project without bringing a lot of unneccessary stuff along with it.

 

Can you now see the problem with your thinking?

 

don't want to create separate objects for each class...

 

You are definately not at this stage yet as you need more knowledge of OO programming, however there are what are called design patterns that are basically solutions to common issues in OO design. There is a solution to the problem in creating objects all over the place in your application called the Registry Pattern. I haven't read over this article completely but i'm sure you can do your own googling to find more info.

 

http://www.phppatterns.com/docs/design/the_registry

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.