Question about a subclass object calling directly the overloaded function within the base class

Source: Internet
Author: User
Ask the subclass object to call directly the overloaded function within the base class?
The question looks like the following code
Class A
{
function A ()
{
$this->nnum = 1024;
}
function Selfclass ()
{
echo "Class A
";
}
}

Class B extends A
{
function Selfclass ()
{
echo "Class B
";
}
}
?>

$obj = new B ();
$obj->selfclass (); Call the function Selfclass () function of Class B
/* How can I use the Selfclass () function of the base class of object obj?
C + + can write similar code $obj->a::selfclass (), then how can PHP be implemented? */
?>

Share to:


------Solution--------------------
Reference: What
about parameter passing?


It's not that troublesome.
Class A
{
function A ()
{
$this->nnum = 1024;
}
function Selfclass ($a, $b, $c, $d, $e, $f)
{

echo "Class A
" . $this->nnum. "
" . " $a, $b, $c, $d, $e, $f ". "
";
}

function __call ($name, $arguments) {
List ($class _name, $function _name) = Explode (' _ ', $name);

if (is_subclass_of ($this, $class _name))
{
Reflect a method
$reflectionMethod = new Reflectionmethod ($class _name, $function _name);

Called with the current object
$reflectionMethod->invokeargs ($this, $arguments);
}


}
}

Class B extends A
{
function B ()
{
$this->nnum = 2048;
}
function Selfclass ()
{
echo "Class B
" . $this->nnum. "
";
}
}

Class C extends B
{
function C ()
{
$this->nnum = 4096;
}
function Selfclass ()
{
echo "Class C
" . $this->nnum. "
";
}
}

$obj = new B ();
$obj->selfclass (); Call the function Selfclass () function of Class B

$obj->a_selfclass (1,2,3,4,5,6,7);

$obj = new C ();
$obj->selfclass (); Call the function Selfclass () function of Class C

$obj->a_selfclass (1,2,3,4,5,6,7);
$obj->b_selfclass ();

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.