0

I am attempting to learn better OOP in PHP and I have been trying to resolve this for a number of hours now and need some assistance. I am using php 5.4 so I believe I can use late static binding.
I have a class called DatabaseObject (database_object.php) which has a function called create that looks like:

    public function create() { 
      echo "in Create() ";
      global $database; echo "<br> table: ".static::$table_name;
      $attributes = $this->sanitized_attributes(); 
      $sql = "INSERT INTO " .static::$table_name." (";
      $sql .= join(", ", array_keys($attributes));
      $sql .= ") VALUES ('";
      $sql .= join("', '", array_values($attributes));
      $sql .= "')"; echo $sql; 
      if($database->query($sql)) {
        $this->id = $database->insert_id();
        return TRUE;
      } else {
        return FALSE;
      }
    }

I am calling this from my Cart class (in a file called cart_id.php)that extends DatabaseObject in a function called add_to_cart() which looks like:

    public function add_to_cart($cart_id,$isbn) { 
      global $database;
      $isbn = $database->escape_value($isbn);
      $amazon = Amazon::get_info($isbn);
      //get cart id if there is not one
      if (empty($cart_id)) {echo " getting cart_id";
        $cart_id = static::get_new_cart_id();
      }
      if(!empty($amazon['payPrice']) && !empty($isbn)) {
        echo "<br> getting ready to save info";
        $cart = new Cart();
        $cart->price = $amazon['payPrice'];
        $cart->qty = $amazon['qty'];
        $cart->cart_id =$cart_id;
        $cart->isbn = $isbn;
        if(isset($cart->cart_id)) { 
          echo " Saving...maybe";
          static::create();
        }
      }

      return $amazon;
    }

The static:create(); is calling the function, but when it gets to

$attributes = $this->sanitized_attributes();

it is not calling the sanitized_attributes function which is in my DatabaseObject class

    protected function sanitized_attributes() {
      echo "<br>in Sanatized... ";
      global $database;
      $clean_attributes = array();
      //Sanitize values before submitting
      foreach($this->attributes() as $key=>$value) {
        $clean_attributes[$key] = $database->escape_value($value);
      }
      return $clean_attributes;
    }

Attributes is

    protected function attributes() {
      //return get_object_vars($this);
      $attributes = array();
      foreach (static::$db_fields as $field) {
        if(property_exists($this, $field)) {
          $attributes[$field] = $this->$field;
        }
       }
       return $attributes;
     }

I get the echo "in create()" as well as the echo "table ".static:table_name, which does show the correct table to save to. I do not get the echo $sql, nor am I getting the "In Sanitized". If I take out the static:create() line, it continues on without a problem and shows me the information in my return $amazon statement. My question is, how am I supposed to correctly call the create function from my add_to_cart() function? If you are going to down vote my question, could you please explain why so I do not duplicate the same error again? Thanks!

2
  • 4
    Please start your OOP adventure with correct indentation. Commented Nov 19, 2014 at 15:48
  • I find it hard to follow your code in little time and with not much context but... are you sure you're using the static keyword correctly?. Apart from declaring static members and methods, it is a relatively "new" thing in php, more like a fix (it is supposed to reach to the top of the class hierarchy from a base class to retrieve the most derived method or value)... Why not just "this->create()"?. Define an abstract "get_table" method in your base class and have the derived classes implement it, in create just do "this->get_table()" and you're done... Commented Nov 19, 2014 at 16:29

1 Answer 1

2

Since you are calling create statically you have to call any other methods of the same class statically as you are not working with an "instance" of the class but a static version of it.

I do not know the structure of the rest of your code but you can either change static::create() to $this->create() and the static calls inside create to calling $this or change the $this->sanitized_attributes() to static::sanitized_attributes().

Also on another note you should refrain from using global variables. Since you going OOP you should practice proper dependency injection and pass those global variables into your classes instead of using global $blah

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for your answer DigitalFiz, I implemented the static::sanitized_attributes (since when I tried $this->create it didn't work) and I got my echo of in Sanitized. My global passes in the connection info, so how should I pass that into my class??
I'm assuming that if $this->create didnt work then you are probably calling add_to_cart statically? Like i said I think you may have cut out to much of the code for anyone to really tell what's wrong. If add_to_cart is called statically then every method in that same class needs to be called statically also when used via the create method. This is one of the ways you can get confused when mixing static/nonstatic methodology. In my opinion you should really avoid static calls unless it is to do simple or specific things.
you are correct, I am calling it by $result = Cart::add_to_cart($cart_id, $isbn); what is the best way to do this?
Maybe the question needs to be revised to give a better picture of what you're trying to accomplish? I think you are mixing things up a bit. It looks like you are using the Cart class for both single items and the cart as a whole. Maybe you should make an Item class for storing information about the item and then make the Cart class all static as I assume you're just using it to interact with the database. So you would generate the Item first then call add_to_cart statically passing in the instance of Item.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.