0

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 $Member1 or Member[2] or so forth in the Fetch_Name function. I hope my question is not complex and illogical.

Many thanks in advance

3
  • You can fill an array with objects from a particular class but there isn't such a thing as an array of type in PHP. Commented Oct 22, 2014 at 9:55
  • I need what you explained. An Array that can be filled by object from a class.But I am stock. Commented Oct 22, 2014 at 9:59
  • The answers provided are pretty good. I was just saying that you cannot really lock an array into accepting only a particular kind of object. Commented Oct 22, 2014 at 10:03

3 Answers 3

3

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but there is one problem here. I don't know how many members do I have in the beginning and the length of $member[] depends on the number of names in the text file. So, how can I build the for loop?
You mostly likely don't want a for loop anyway. Your extractor class needs to parse the file and figure out how many names there are and instantiate as many Members as necessary. I can't tell you how to write your parser (which is what you're really asking) since I have no idea what the requirements are. Ask a separate question about that.
1

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;
}

Comments

1

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.

L.E: 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();

  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.

2 Comments

Tnx Ares, but what you sa is a class whose properties are array, right?
yes. The object gets created and you can hold many results in the same property. I though this is what you wanted :) Test it, and if not, please be more explicit, so I can help you better :D

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.