1internet Posted March 12, 2013 Share Posted March 12, 2013 (edited) If I have class class1 { public function variable($str) { $this->str = $str } } class class2 { public function get() { echo class1->str; } } How can class2 get the 'str' variable from class1, do I need to create an extension? So would would instantiate it with $str = new class1(); $var = $str->str('test'); $str1 = new class2(); $str1->get(); Edited March 12, 2013 by 1internet Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/ Share on other sites More sharing options...
serien Posted March 12, 2013 Share Posted March 12, 2013 The best way, I would say, would definitely be to make class2 extend class1 class class1 { public function variable($str) { $this->str = $str } } class class2 extends class1 { public function get() { echo $this->str; // because of extension, str is now a variable of class2 as well and can be called using $this } } Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418114 Share on other sites More sharing options...
trq Posted March 12, 2013 Share Posted March 12, 2013 The best way, I would say, would definitely be to make class2 extend class1Unless class2 is a type of class1 that would be incorrect. The best method is to use dependency inject. Either via the __construct, or as an argument to the method itself. eg; <?php class class2 { protected $class1; public function __construct(class1 $class1) { $this->class1 = $class1; } public function get() { echo $this->class1->str; } } $c2 = new class2(new class1); $c2->get(); [/code] or [code] <?php class class2 { public function get(class1 $class1) { echo $class1->str; } } $c2 = new class2; $c2->get(new class1); Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418141 Share on other sites More sharing options...
AyKay47 Posted March 12, 2013 Share Posted March 12, 2013 (edited) Unless Class1 extends Class2, I would encapsulate the str variable inside Class1 and implement a public getter method. class class1 { protected $str; public function variable($str) { $this->str = $str } public function get() { return $this->str; } } class class2 { protected class1; public function get(class1 $class1) { $this->class1 = $class1; echo $class1->get(); } } $c2 = new class2; $c2->get(new class1); Edited March 12, 2013 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418150 Share on other sites More sharing options...
ignace Posted March 12, 2013 Share Posted March 12, 2013 Like thorpe already mentioned inject the class1 into class2 through the constructor and retrieve the string value through a getter method. Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418154 Share on other sites More sharing options...
serien Posted March 12, 2013 Share Posted March 12, 2013 Ahh, yep, screwed that up in my first answer XD. Definitely assumed that class2 was of type class1. Thanks guys ^^; 1internet, theirs will probably work better for you. That's what I get for answering questions at 3am haha. Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418187 Share on other sites More sharing options...
1internet Posted March 12, 2013 Author Share Posted March 12, 2013 What about if I returned the variable from the class1, and executed from class 2 from there. Perhaps if I tell you what I am trying to accomplish here, there may be a better way. I want to create a form with oop. But I also want the form to update the db, and go through a validation process. The form will have an arbitrary number of fields with different field names. I have to create the form first, so that the field names and number of fields is known. Then I have to create the validation by pulling these variables from the form. And although the validation will be created secondly, it must be displayed first on the page, i.e. before the header info. Any better suggestions on how to go about this? Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418234 Share on other sites More sharing options...
AyKay47 Posted March 12, 2013 Share Posted March 12, 2013 OOP goes way beyond simply throwing some classes together. My advice would be to get a firm grasp on the fundamentals of sequential/procedural styles before dabbling in OOP. Then when you are ready to start learning OOP, begin with the fundamentals and build off of that by delving into OOP design patterns/frameworks etc. Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418241 Share on other sites More sharing options...
1internet Posted March 12, 2013 Author Share Posted March 12, 2013 Sure I understand, I have got into MVC, and understand it, but I am not quite there yet. I haven't quite grasped OOP yet, and still thinking too procedural, but I am trying to get there. Starting with a "simple" form. Any advice and tips are appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418246 Share on other sites More sharing options...
AyKay47 Posted March 12, 2013 Share Posted March 12, 2013 (edited) No offense, but if you have not yet grasped OOP, I sincerely doubt that you have grasped MVC to its full potential. There is no simple answer to this that will be 100% correct without going into vast detail. Edited March 12, 2013 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418247 Share on other sites More sharing options...
1internet Posted March 12, 2013 Author Share Posted March 12, 2013 Sure, I am far from MVC full potential. So once you fully grasp OOP, do you ever even use procedural any more? I am trying to do as much OOP as possible until I truly get it. Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418250 Share on other sites More sharing options...
AyKay47 Posted March 12, 2013 Share Posted March 12, 2013 The thing with OOP is, it requires more resources to run and is much more time consuming then procedural. While I always want to use OOP for every project I gain, there are some that just don't warrant the time to do so. It's a case by case sort of deal. Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418268 Share on other sites More sharing options...
trq Posted March 13, 2013 Share Posted March 13, 2013 No offense, but if you have not yet grasped OOP, I sincerely doubt that you have grasped MVC to its full potential. No ofense, but MVC is a very simple pattern that can easily be implemented without any OOP. Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418279 Share on other sites More sharing options...
akphidelt2007 Posted March 13, 2013 Share Posted March 13, 2013 What about if I returned the variable from the class1, and executed from class 2 from there. Perhaps if I tell you what I am trying to accomplish here, there may be a better way. I want to create a form with oop. But I also want the form to update the db, and go through a validation process. The form will have an arbitrary number of fields with different field names. I have to create the form first, so that the field names and number of fields is known. Then I have to create the validation by pulling these variables from the form. And although the validation will be created secondly, it must be displayed first on the page, i.e. before the header info. Any better suggestions on how to go about this? There's probably no reason to do this for a form class unless you want to extend input types which isn't really necessary. And I have no clue what you mean by this "And although the validation will be created secondly, it must be displayed first on the page, i.e. before the header info." You can create, validate, and submit a form to the DB all in one class. But we'd have to know more about how you have stuff set up in order to give you better advice. Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418295 Share on other sites More sharing options...
AyKay47 Posted March 13, 2013 Share Posted March 13, 2013 (edited) No ofense, but MVC is a very simple pattern that can easily be implemented without any OOP. Right, but since this topic is in the context of OOP, we are talking about OOP MVC which is a little more difficult to implement correctly. Edited March 13, 2013 by AyKay47 Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418351 Share on other sites More sharing options...
trq Posted March 13, 2013 Share Posted March 13, 2013 Right, but since this topic is in the context of OOP, we are talking about OOP MVC which is a little more difficult to implement correctly.I don;t think so. It's a very simple pattern. Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418379 Share on other sites More sharing options...
ignace Posted March 13, 2013 Share Posted March 13, 2013 (edited) I don;t think so. It's a very simple pattern.Indeed. It's even possible for some to look at MVC without seeing it. Not implying the pattern is hard just that it is easy for someone who does not fully grasp MVC to not see it because it has become so boilerplate and those that do have grasped it can toy with it however they want.class UserActions { private $userDbTable; public function register(RegisterForm $data) { .. return new RegistrationPage($data); } public function login(LoginForm $data) { $user = $this->userDbTable->findByEmailPassword($data->getEmail(), $data->getPassword()); if ($user) { return new RedirectTo('profile'); } return new LoginPage($data); } } class UserDbTable { public function findByEmailPassword($email, $password) { .. } } .. Edited March 13, 2013 by ignace Quote Link to comment https://forums.phpfreaks.com/topic/275535-getting-variables-in-a-class-from-another/#findComment-1418390 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.