Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New to the CF scene
    Join Date
    Mar 2014
    Posts
    3
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Which way around is more "correct"?

    Say, for example, I have 2 classes; Customer and Address. If I want to load an Address when I'm working with a Customer do I place the code within the Customer or Address class?

    Method #1: place a method in the Customer class:
    PHP Code:
    public function Get_Address(){
         
    $qry = new Query("SELECT id FROM address WHERE customer_id=:id",array("id"=>$this->id));
          if(
    $row $qry->Fetch()){
              return new 
    Address($row['id']);
          }
          return 
    false;


    Method #2: use static method in Address class:
    PHP Code:
     public static function Get_From_Customer_Id($id){
          
    $qry = new Query("SELECT * FROM address WHERE customer_id=:id",array("id"=>$id));
          if(
    $row $qry->Fetch()){
              return new 
    self($row['id']);
          }
          return 
    false;

    I come across this scenario quite often and up until recently was using Method#2 but I think Method#1 is probably the more correct???

    Thanks in advance!

  • #2
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,439
    Thanks
    62
    Thanked 537 Times in 524 Posts
    Personally I wouldn't have two classes for something that is so closely related. I'd just use one class and have everything inside that instead.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    I myself would keep them separate. A customer isn't directly related to an Address, and more importantly, an Address can belong to many customers. I would also assume that a customer could have more than one address, so I would build on a many to many relation model.
    To answer your question though, Address should be responsible for all actions related to address. Depending on the overall structure, I would presume that an address requires an owning Customer, so that there will always be a way to fetch the information based on the customer. Regardless of it you are using a one to one or many to many (with a third class), I would construct an Address object based on the customer id (or m2m an addressid derived from the customerid) and then load as a part of construction. Static doesn't make any sense to me in this regard, but may be useful in a third class should you load a collection of address or customer objects.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    james.1 (03-26-2014)


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •