调用父类 PHP 类上的子类方法

huangapple go评论61阅读模式
英文:

Calling Sub Class Method On Parent PHP Class

问题

I have 2 PHP classes that are related to each other. Instead of using __construct, I tried to use extends to combine these 2 classes. However, the subclass method can't be called from the parent class. There's neither a result nor an error displayed, but there is a warning about reaching the memory limit.

Here is my example code:

<?php
class Me
{

    public $you;

    public function __construct()
    {
        $this->you = new You;
    }

    public function Hi()
    {
        return 'Hi You';
    }

    public function WhatYouSaid()
    {
        return $this->you->Me();
    }

}

class You extends Me
{

    public function Me()
    {
        return 'Yes Me';
    }
}

$talk = new You;
print_r($talk->WhatYouSaid());

I received the error:

Fatal error: Allowed memory size of xxx bytes exhausted (tried to allocate xx bytes)

Based on @Nigel Ren's answer, I found a solution. Here's another example:

<?php
class DataValidate {

    public $data;

    public function __construct() {
        $this->data = new Data;
    }

    public function inputValidate($newData) {
        // Verifying
        if ($this->data->get_data($newData['Identifier'], 'nameId')) {
            return false; // Already exists
        }
        return $newData;
    }

}

class Data {

    public $db;
    public $validate;

    public function __construct() {
        $this->db = new Database;
        $this->validate = new DataValidate;
    }

    public function get_data($identifier, $col) {
        return $this->db->select($identifier, $col='*');
    }

    public function inputValidate($newData) {
        return $this->validate->inputValidate($newData);
    }
}

$data = new Data;
print_r($data->inputValidate($some_id));

请注意,这些代码示例中的一些部分可能需要进一步调整以解决您的问题。

英文:

I have 2 PHP class that related each other, rather than using __construct, I tryin to using extends to combine these 2 class. But why sub class method cant called from parent class? There's no either result or error displayed but warning about reach memory limit.

here my example code:

&lt;?php
class Me
{

    public $you;

    public function __construct()
    {
        $this-&gt;you = new You;
    }

    public function Hi()
    {
        return &#39;Hi You&#39;;
    }

    public function WhatYouSaid()
    {
        return $this-&gt;you-&gt;Me();
    }

}

class You extends Me
{

    public function Me()
    {
        return &#39;Yes Me&#39;;
    }
}

$talk = new You;
print_r($talk-&gt;WhatYouSaid());

https://onlinephp.io/c/3d84d

I received error:
> Fatal error: Allowed memory size of xxx bytes exhausted (tried to allocate xx bytes)

Based on @Nigel Ren answer i found solution here another example:

&lt;?php
 class DataValidate {

	public $data;	

  public function __construct() {
     $this-&gt;data = new Data;
  }

public	function inputValidate($newData)
	{
		// verifying
		if ($this-&gt;data-&gt;get_data($newData[&#39;Identifier&#39;], &#39;nameId&#39;))
		{
			return false; // alredy exists
		}
		return $newData;
	}
	
}




class Data {

	public $db;	
	public $validate;

  public function __construct() {
     $this-&gt;db = new Database;
     $this-&gt;validate = new DataValidate; 
  }	

public	function get_data($identifier, $col)
		{
			return $this-&gt;db-&gt;select($identifier, $col=&#39;*&#39;);
		}
		
public  function inputValidate($newData)
{
	
	return $this-&gt;validate-&gt;inputValidate($newData);
}
}



$data = new Data;
print_r($data-&gt;inputValidate($some_id));

答案1

得分: 1

"Each time you create a new object or type You, this calls the constructor in Me. This then creates a new object of type You, this calls the constructor in Me ..."
每次创建一个新对象或输入You,都会调用Me中的构造函数。然后创建一个新的You类型对象,这会再次调用Me中的构造函数...

"So you quickly run out of memory as it's creating loads of new objects."
因此,由于创建了大量新对象,您很快会耗尽内存。

"There is also a case of having a different method name, I have assumed that you meant to have the same name for the 2 output methods. You wouldn't normally introduce a new method in a sub class."

还有一种情况是方法名称不同,我假设您是希望两个输出方法具有相同的名称。通常情况下,您不会在子类中引入新的方法。

"So this code just calls the method on the current object rather than creating a new object to use..."

因此,这段代码只是调用当前对象上的方法,而不是创建一个新对象来使用...

class Me {
    public function Hi()
    {
        return 'Hi You';
    }

    public function WhatYouSaid()
    {
        return $this->Hi();
    }	
}

class You extends Me{

    public function Hi()
    {
        return 'Yes Me';
    }
}

$talk = new You;
print_r($talk->WhatYouSaid());

如果你只想实现从另一个类中调用方法的想法,那么使用extend并没有帮助。只需创建一个具有public方法的类,允许您进行调用。

然后,您创建Me的实例而不是You,然后进行调用...

class Me
{
    public $you;

    public function __construct()
    {
        $this->you = new You;
    }

    public function Hi()
    {
        return 'Hi You';
    }

    public function WhatYouSaid()
    {
        return $this->you->Me();
    }
}

class You
{
    public function Me()
    {
        return 'Yes Me';
    }
}

$talk = new Me;
print_r($talk->WhatYouSaid());
英文:

Each time you create a new object or type You, this calls the constructor in Me. This then creates a new object of type You, this calls the constructor in Me ...

So you quickly run out of memory as it's creating loads of new objects.

There is also a case of having a different method name, I have assumed that you meant to have the same name for the 2 output methods. You wouldn't normally introduce a new method in a sub class.

So this code just calls the method on the current object rather than creating a new object to use...

class Me {
    public	function Hi()
	{
		return &#39;Hi You&#39;;
	}

    public	function WhatYouSaid()
	{
		return $this-&gt;Hi();
	}	
	
}

class You extends Me{

    public	function Hi()
    {
		return &#39;Yes Me&#39;;
	}
}

$talk = new You;
print_r($talk-&gt;WhatYouSaid());

Edit:
If you just want to make the idea of calling a method from another class, then using extend doesn't help you at all. It just needs to be a class which has a public method to allow you to call.

Then you create an instance of Me rather than You and make the call...

class Me
{
    public $you;

    public function __construct()
    {
        $this-&gt;you = new You;
    }

    public function Hi()
    {
        return &#39;Hi You&#39;;
    }

    public function WhatYouSaid()
    {
        return $this-&gt;you-&gt;Me();
    }
}

class You
{
    public function Me()
    {
        return &#39;Yes Me&#39;;
    }
}

$talk = new Me;
print_r($talk-&gt;WhatYouSaid());

答案2

得分: 0

问题实际上在于,每次你尝试创建一个 Me 对象时,它会实例化一个 You 对象(因为你在 Me 的构造函数中设置了这样做)。

由于 You 继承了 Me,当你实例化一个 You 对象时,它会先执行 Me 构造函数中的代码,因此你会陷入一个无休止的循环。然后你会耗尽内存。

英文:

Actually the problem is that, each time you try to create a Me object, it instantiate a You object (as you set in Me's constructor).

As You extend Me, when you instantiate a You object, it starts by executing the code in Me's constructor, so you go into a non-stop loop. Then you run out of memory.

huangapple
  • 本文由 发表于 2023年2月26日 18:57:14
  • 转载请务必保留本文链接:https://go.coder-hub.com/75571500.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定