简体   繁体   English

如何在PHP中创建类型为“ class”的数组?

[英]How can I make an array of type “class” in PHP?

I have the following class with several properties and a method in PHP (This is simplified code). 我有以下带有几个属性的类和PHP中的方法(这是简化的代码)。

class Member{
    public $Name;
    public $Family;

    public function Fetch_Name(){
      for($i=0;$i<10;$i++){
        $this[$i]->$Name = I find the name using RegExp and return the value to be stored here;
        $this[$i]->Family = I find the family using RegExp and return the value to be stored here;
      }
    }//function
}//class

In the function Fetch_Name(), I want to find all the names and families that is in a text file using RegExp and store them as properties of object in the form of an array. 在函数Fetch_Name()中,我想使用RegExp查找文本文件中的所有名称和族,并将它们作为对象的属性以数组的形式存储。 But I don't know how should I define an array of the Member. 但是我不知道如何定义成员数组。 Is it logical or I should define StdClass or 2-dimension array instead of class? 逻辑还是我应该定义StdClass或2维数组而不是class?

I found slightly similar discussion here , but a 2 dimensional array is used instead of storing data in the object using class properties. 我在这里发现了一些类似的讨论,但是使用了二维数组,而不是使用类属性将数据存储在对象中。

I think my problem is in defining the following lines of code. 我认为我的问题在于定义以下代码行。

$Member = new Member();
$Member->Fetch_name();

The member that I have defined is not an array. 我定义的成员不是数组。 If I do define it array, still it does not work. 如果我确实定义它数组,它仍然无法正常工作。 I did this 我做了这个

$Member[]= new Member();

But it gives error 但这给了错误

Fatal error: Call to a member function Fetch_name() on a non-object in 致命错误:在非对象中调用成员函数Fetch_name()

if I give $Member[0]= new Member() then I don't know how to make $Member 1 or Member[2] or so forth in the Fetch_Name function. 如果我给$ Member [0] = new Member(),那么我不知道如何在Fetch_Name函数中制作$ Member 1或Member [2]等等。 I hope my question is not complex and illogical. 我希望我的问题不是复杂和不合逻辑的。

Many thanks in advance 提前谢谢了

A Member object represents one member. Member对象代表一个成员。 You're trying to overload it to represent or handle many members, which doesn't really make sense. 您正在尝试使它过载以表示或处理许多成员,这实际上没有任何意义。 In the end you'll want to end up with an array that holds many Member instances, not the other way around: 最后,您将需要一个包含许多Member实例的数组,而不是相反:

$members = array();
for (...) {
    $members[] = new Member($name, $family);
}

Most likely you don't really need your Member class to do anything really; 您很可能不需要真正的Member类来真正任何事情。 the extraction logic should reside outside of the Member class, perhaps in an Extractor class or something similar. 提取逻辑应该驻留在Member类之外,可能在Extractor类中或类似的东西中。 From the outside, your code should likely look like this: 从外部看,您的代码应该看起来像这样:

$parser  = new TextFileParser('my_file.txt');
$members = $parser->extractMembers();

I think you should have two classes : 我认为您应该上两节课:

  • The first one, Fetcher (or call it as you like), with your function. 第一个是Fetcher(或随意调用),带有函数。
  • The second one, Member, with the properties Name and Family. 第二个成员,具有属性Name和Family。

It is not the job of a Member to fetch in your text, that's why I would make another class. 提取您的文本不是会员的工作,这就是为什么我要开设另一个班级。

In your function, do your job, and in the loop, do this : 在您的函数中,执行您的工作,然后在循环中,执行以下操作:

for($i = 0; $i < 10; ++$i){
    $member = new Member();
    $member->setName($name);
    $member->setFamily($family);
    // The following is an example, do what you want with the generated Member
    $this->members[$i] = $member;
}

The problem here is that you are not using the object of type Member as array correctly. 这里的问题是您没有正确使用Member类型的对象作为数组。 The correct format of your code would be: 您的代码的正确格式为:

class Member{
    public $Name;
    public $Family;

    public function Fetch_Name(){
      for($i=0;$i<10;$i++){
        $this->Name[$i] = 'I find the name using RegExp and return the value to be stored here';
        $this->Family[$i] = 'I find the family using RegExp and return the value to be stored here';
      }
    }
}

First, $this->Name not $this->$Name because Name is already declared as a member variable and $this->Name[$i] is the correct syntax because $this reference to the current object, it cannot be converted to array, as itself. 首先, $this->Name不是$this->Name $this->$Name因为Name已经被声明为成员变量,并且$this->Name[$i]是正确的语法,因为$this引用当前对象,因此无法转换阵列本身。 The array must be contained in the member variable. 该数组必须包含在成员变量中。

LE: I might add that You are not writing your code according to PHP naming standards. LE:我可能会补充说您不是按照PHP命名标准编写代码。 This does not affect your functionality, but it is good practice to write your code in the standard way. 这不会影响您的功能,但是以标准方式编写代码是一种很好的做法。 After all, there is a purpose of having a standard. 毕竟,有一个标准的目的。

Here you have a guide on how to do that. 在这里,您将获得有关如何执行此操作的指南。

And I would write your code like this: 我会这样写你的代码:

class Member{
    public $name;
    public $family;

    public function fetchName(){
      for($i=0;$i<10;$i++){
        $this->name[$i] = 'I find the name using RegExp and return the value to be stored here';
        $this->family[$i] = 'I find the family using RegExp and return the value to be stored here';
      }
    }
}

L.E2: Seeing what you comented above, I will modify my answer like this: L.E2:看到您上面提到的内容,我将像这样修改我的答案:

So you are saying that you have an object of which values must be stored into an array, after the call. 因此,您说的是,在调用之后,您有一个必须将值存储到数组中的对象。 Well, after is the key word here: 好了, after是这里的关键词:

  1. Initialize your object var: $member = new Memeber(); $memebr->fechNames(); 初始化您的对象var: $member = new Memeber(); $memebr->fechNames(); $member = new Memeber(); $memebr->fechNames();

  2. Initialize and array in foreach 在foreach中初始化和数组

     $Member = new Member(); foreach ($Member->Name as $member_name){ $array['names'][] = $member_name; } foreach ($Member->Family as $member_family) { $array['family'][] = $member_family; } var_dump($array); 

Is this more of what you wanted? 这更多是您想要的吗?

Hope it helps! 希望能帮助到你!
Keep on coding! 继续编码!
Ares. 战神。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM