How to call a function correctly in PHP OOP -


i attempting learn better oop in php , have been trying resolve number of hours , need assistance. using php 5.4 believe can use late static binding.
have class called databaseobject (database_object.php) has function called create looks like:

    public function create() {        echo "in create() ";       global $database; echo "<br> table: ".static::$table_name;       $attributes = $this->sanitized_attributes();        $sql = "insert " .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 calling cart class (in file called cart_id.php)that extends databaseobject in function called add_to_cart() 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 not 1       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 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(); calling function, when gets to

$attributes = $this->sanitized_attributes(); 

it not calling sanitized_attributes function in databaseobject class

    protected function sanitized_attributes() {       echo "<br>in sanatized... ";       global $database;       $clean_attributes = array();       //sanitize values before submitting       foreach($this->attributes() $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 $field) {         if(property_exists($this, $field)) {           $attributes[$field] = $this->$field;         }        }        return $attributes;      } 

i echo "in create()" echo "table ".static:table_name, show correct table save to. not echo $sql, nor getting "in sanitized". if take out static:create() line, continues on without problem , shows me information in return $amazon statement. question is, how supposed correctly call create function add_to_cart() function? if going down vote question, please explain why not duplicate same error again? thanks!

since calling create statically have call other methods of same class statically not working "instance" of class static version of it.

i not know structure of rest of code can either change static::create() $this->create() , static calls inside create calling $this or change $this->sanitized_attributes() static::sanitized_attributes().

also on note should refrain using global variables. since going oop should practice proper dependency injection , pass global variables classes instead of using global $blah


Comments

Popular posts from this blog

java - Oracle EBS .ClassNotFoundException: oracle.apps.fnd.formsClient.FormsLauncher.class ERROR -

c# - how to use buttonedit in devexpress gridcontrol -

nvd3.js - angularjs-nvd3-directives setting color in legend as well as in chart elements -