英文:
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:
<?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 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:
<?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; // alredy 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));
答案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 '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());
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->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());
答案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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论