简体   繁体   中英

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).

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. 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?

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

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. I hope my question is not complex and illogical.

Many thanks in advance

A Member object represents one 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:

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

Most likely you don't really need your Member class to do anything really; the extraction logic should reside outside of the Member class, perhaps in an Extractor class or something similar. 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.
  • The second one, Member, with the properties Name and 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. 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. 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. 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:

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:

  1. Initialize your object var: $member = new Memeber(); $memebr->fechNames(); $member = new Memeber(); $memebr->fechNames();

  2. Initialize and array in 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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