OOP Question

I have this public function:


public function a(){
 foreach($_POST['idClient'] as $key=>$value) {
		
 print('sending username: '.$_POST['idClient'][$key].'<br />');
 print('sending username: '.$_POST['username'][$key].'<br />');
 print('sending password : '.$_POST['password'][$key].'<br />');
 print('-------------'.'<br />');

    }
  }


my question; if I include another class method in the above’s class method and want to do the print statements on this other class method how would I do this?

So above class would this:


public function a(){
 foreach($_POST['idClient'] as $key=>$value) {
		
 $b_class = new email_approve_con();
$b_class->b();

    }
  }


and the class I want to have access to class a methods POST vars would look like so:


public function b(){
  print('sending username: '.$_POST['idClient'][$key].'<br />');
 print('sending username: '.$_POST['username'][$key].'<br />');
 print('sending password : '.$_POST['password'][$key].'<br />');
 print('-------------'.'<br />');
  }


hope this makes sense :blush:

So looking at your code, am I right in saying object a is telling object b to do something with one part of an array coming from your POST values?

Well in that case I would have class a only pass on the part of the array to b that it needs in order to do its job - get class a to do your conditional checks and maybe simple isset() validation etc then only give class b what it needs, nothing more.


$b_class = new email_approve_con();
$b_class->b($arr);

otherwise why does object a exist?

Or, is your question to do with how to extract just one item from your POST array?

If so, what does say, 2 items from your POST array look like?

Hey cups, your first guess is right. In term of method design I thought there was a better way of doing this…

Judging by the name the class email_approve_con goes on and sends an email to one person? Am I right?

If so what other things does the class which contains method a have to do? Giving it a better name might help, even if the whole thing is contrived.

FormProcessor?

So FormProcessor is now fully dependent upon having access to your email_approve_con class.

If your email_approve_con class is very generic and is used by other applications/classes then there is nothing wrong with that at all.

If email_approve_con class is only ever to be used with this FormProcessor, and only contains one single method, then why bother creating a new dependency?

(ie methods a and b could be parts of the same class unless you can justify to yourself why they should not)

These are the types of questions you need to answer, and as your thinking ebbs and flows in different directions - then you alter your code accordingly.

It is dead easy to do this when just starting on a set of classes, so make the most of it and experiment with different ways of doing things and explore the various OO principles that you are toying with.

In my last reply I intended to say, if the email_approve_con->b() depends on being given, say 3 separate items and they are all mandatory or email_approve_con->b() will fail, then your a class could shoulder some/all/none of the responsibility for making sure the email_approve_con has what it needs to execute.


$b = new email_approve_con;
$b->setIdClient($_POST[$k]['clientID']);
$b->setUsername($_POST[$k]['username']);
$b->setPassword($_POST[$k]['password']);

// or

$b = new email_approve_con;
$b->b($clientID, $username, $password);

// or

$arr = array(
'clientID'=>$_POST[$k]['clientID'],
'username'=>$_POST[$k]['username'],
'password'=>$_POST[$k]['password']
);

$b = new email_approve_con;
$b->b($arr);


That is the happy path, with all vars actually set and previously filtered and cleansed, of course.

thanks for the run down cups 1lov :slight_smile: