LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-18-2006, 09:40 AM   #1
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Rep: Reputation: 15
Question Php Coding Question


I'm wondering if there is some way in php to return the name of the child class calling a method on a parent class?

Example:

<?php

Class parentObject
{
Public static Function returnClassName() {
return __CLASS__;

}

}

Class childObject extends parentObject
{

}

echo childObject::returnClassName();

?>

This returns parentObject, I would like it to return childObject as that is the child class that is actually invoking the method. I am wondering if there is a way to have the method return the name of the Class that invoked it, not the parent class it actually physically lives in?

Any help would be much appreciated!

Thanks,
KARL.
 
Old 09-18-2006, 10:37 AM   #2
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
Question

I came up with this half working solution:

<?php

Class parentObject {

Static $ObjDegree = 3;

Public Function concreteReturnClassVariable() {
return get_class($this);
}

Public static Function staticreturnClassVariable() {
return get_class();
}

}

Class childObject extends parentObject {
Static $ObjDegree = 10;
}

$obj = new childObject();
echo $obj->concreteReturnClassVariable();

echo childObject::staticreturnClassVariable();

?>

I can get a method to return the name of the child class calling a method in the parent function if I use non-static functions after creating an instance of that child class and get_class($this).

However, it seems public static functions do not support get_class($this) so I can not do the same thing with a static call:

echo childObject::staticreturnClassVariable();
 
Old 09-18-2006, 04:17 PM   #3
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 9,553
Blog Entries: 4

Rep: Reputation: 3437Reputation: 3437Reputation: 3437Reputation: 3437Reputation: 3437Reputation: 3437Reputation: 3437Reputation: 3437Reputation: 3437Reputation: 3437Reputation: 3437
If you don't find an obvious language mechanism that lets you do something .. reconsider your design.

If you need to know "which child is calling my method" in order to make something work, it's a dead giveaway (imho) that something about the arrangement is wonked and you're creating code that you're going to regret.

(I've done it myself many times. "I wrote this?!" "Bad programmer! No biscuit!!" )
 
Old 09-18-2006, 04:25 PM   #4
joelhop
Member
 
Registered: Mar 2004
Location: Pennsylvania::USA
Distribution: Fedora Core 6
Posts: 100

Original Poster
Rep: Reputation: 15
Yeah I agree the initial discussion that raised this question was about a hackish way to do something. I was just wondering if it was possible, but from what I've read it doesn't seem likely.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Php Coding Question joelhop Programming 2 08-11-2006 01:33 PM
Php Coding Question: Static Functions joelhop Programming 2 08-09-2006 12:17 PM
Php Coding Question joelhop Programming 5 08-07-2006 03:09 PM
PHP Coding Question joelhop Programming 12 07-24-2006 06:52 PM
PHP Coding Question joelhop Programming 2 07-20-2006 07:16 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration