Ask the subclass object to call directly the overloaded function within the base class?

Source: Internet
Author: User
Tags php and mysql
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? */
?>


Reply to discussion (solution)

What I'm asking for is not like this answer.
Class B extends A
{
function Selfclass ()
{
echo "Class B
";
}

function Selfclassx ()
{
Parent::selfclass ();
}
}

PHP does not provide such a syntax, but can be simulated, but it is not recommended to use

 
  Nnum = 1024;} function Selfclass () {echo ' class A
" . $this->nnum. "
";} function __call ($name, $arguments) {list ($class _name, $function _name) = Explode (' _ ', $name); if (Is_subclass_of ($this, $ class_name) {if ($class _name = = __class__) {self:: $function _name ();} else {$class _name:: $function _name ();}}}} 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 of Class B Selfclass () function $obj->a_selfclass (); $obj = new C (); $obj->selfclass (); Call the class C function Selfclass () function $obj->a_selfclass (); $obj->b_selfclass ();

This syntax $class_name:: $function _name ();
and self:: $function _name (), the use of $function_name () is a static function.

 
  Nnum = 1024x768;    }    function Selfclass ()    {        echo ' class A
" . $this->nnum. "
"; } function __call ($name, $arguments) { list ($class _name, $function _name) = Explode (' _ ', $name); if (Method_exists ($ This, $function _name) && is_a ($this, $class _name)) {if (Is_subclass_of ($this, $class _name)) {A:: $function _name ();} else{$this $function _name ();}}} Class B extends a{ function B () { $this->nnum = 2048; } function Selfclass () { echo ' class B
" . $this->nnum. "
"; }

I don't know what you're doing.
But at least 21 lines of a:: $function _name ();
Should write $class _name:: $function _name ();
Otherwise, it's not universal.

$class _name:: $function _name (); There is a grammatical error in this notation. You can't write like this.
$class _name can only be parsed into strings, and this syntax cannot be used. You think it's good, but it's not possible.

This is not just a good wish! Test proof writing $class _name:: $function _name () is correct (php5.4.12)

However, it is important to note that:
Whether it's writing $class _name:: $function _name ()
Or writing a:: $function _name ()
Do not conform to PHP syntax, because the need to $function _name is a static method, and here is not

And here PHP did not error, is obviously a BUG

The wording of your proposed test environment in my side is not possible, and you say no, I can test through this side.
I have measured a:: $function _name () is possible, this call does not require $function _name is a static method.
and $class_name:: $function _name () I've measured that I can't parse $class_name into classes.
Can only be parsed as a string. Is my PHP version too old!!!

A::selfclass (); Strict Standards:non-static Method A::selfclass () should not being called statically this is a normal syntax error (masked)
$function _name = ' selfclass ';
A:: $function _name (); It's not normal to report grammatical errors here.

There should be no double standard in a system!

Mine is PHP Version 5.2.6.

I found in a book A:: $function _name (); a use case similar to this syntax

This is the 4th edition of PHP and MySQL programming.

Class Name:: Method Name This is the way it is, and it is only when the php5.3 began to have restrictions.

I used to use C + +, learning PHP is always the inertia that some usage should be similar.

C + + is compiled and executed, so the source code is scanned a few times it doesn't matter
PHP is interpreted to perform, and multiple scans will affect execution efficiency.

If you write the interpreter yourself, you'll find that there are a lot of ambiguity problems that can't be solved in two scans.

The moderator mentions that the two question of righteousness is indeed the focus. Want to ask the moderator, I originally was doing C + + game development, want to change to do the site
As a PHP programmer, you do not need to learn JavaScript, and the basic HTML to learn to what extent.

Direct use of Reflection bar, php5.2 php5.3 can be tested through

 
  Nnum = 1024;} function Selfclass () {echo ' class A
" . $this->nnum. "
";} 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);//call $ with the current object Reflectionmethod->invoke ($this);}} 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 of Class B Selfclass () function $obj->a_selfclass (); $obj = new C (); $obj->selfclass (); Call the class C function Selfclass () function $obj->a_selfclass (); $obj->b_selfclass ();

So what about parameter passing?

The firing method is a good idea, but the parameter problem is more troublesome.

I think I could probably use the Evla () function to solve it.

Wrong write, should be void eval (string code_str);

So 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 ();

Oh, it's a good deal. Thanks Hnxxwyq

  • Related Article

    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.