delickate Posted January 10, 2012 Share Posted January 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/ Share on other sites More sharing options...
trq Posted January 10, 2012 Share Posted January 10, 2012 Children inherit from there parents, not the other way around. The parent doesn't even know the child exists. Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306021 Share on other sites More sharing options...
mrdhood Posted January 10, 2012 Share Posted January 10, 2012 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'; } } Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306027 Share on other sites More sharing options...
delickate Posted January 10, 2012 Author Share Posted January 10, 2012 Thanks guys, ok. can i inherit a class from more than 1 class in PHP? e.g class B { ... } class C { ... } //like this class A extends b,c { } is it possible? Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306033 Share on other sites More sharing options...
Adam Posted January 10, 2012 Share Posted January 10, 2012 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? Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306037 Share on other sites More sharing options...
Andy-H Posted January 10, 2012 Share Posted January 10, 2012 If a class needs access to the method of a child, surely the method of the child class should be in the parent class anyway? Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306042 Share on other sites More sharing options...
delickate Posted January 10, 2012 Author Share Posted January 10, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306068 Share on other sites More sharing options...
trq Posted January 10, 2012 Share Posted January 10, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306071 Share on other sites More sharing options...
JonnoTheDev Posted January 10, 2012 Share Posted January 10, 2012 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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306074 Share on other sites More sharing options...
mrdhood Posted January 10, 2012 Share Posted January 10, 2012 Slightly off-topic, but PHP4 mrdhood? What? Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306289 Share on other sites More sharing options...
KevinM1 Posted January 11, 2012 Share Posted January 11, 2012 Slightly off-topic, but PHP4 mrdhood? What? The use of the 'var' keyword is deprecated. It's a PHP 4 construct. Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306294 Share on other sites More sharing options...
Adam Posted January 11, 2012 Share Posted January 11, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306295 Share on other sites More sharing options...
mrdhood Posted January 11, 2012 Share Posted January 11, 2012 Oh, I've never gotten that message so I didn't even notice. It was just a quick little example anyways lol. Good to know though Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306300 Share on other sites More sharing options...
delickate Posted January 11, 2012 Author Share Posted January 11, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306342 Share on other sites More sharing options...
trq Posted January 11, 2012 Share Posted January 11, 2012 We understand what your trying to do. We are telling you however, that it is not good nor proper design. It makes no sense from an OOP perspective. If you want to keep trying to go down this path your on your own. Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306355 Share on other sites More sharing options...
JonnoTheDev Posted January 11, 2012 Share Posted January 11, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/254701-calling-child-method-from-parent/#findComment-1306378 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.