Jump to content

getting variables in a class from another


1internet

Recommended Posts

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 by 1internet
Link to comment
Share on other sites

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
    }

}
Link to comment
Share on other sites

The best way, I would say, would definitely be to make class2 extend class1

Unless 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);
Link to comment
Share on other sites

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 by AyKay47
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by AyKay47
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

 

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by AyKay47
Link to comment
Share on other sites

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 by ignace
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.