1

anyone has an idea how to create a class in PHP then when called loads an array for example

$cart = new Cart(); //where Cart is the Class Name
print_R($cart); //print the constructor

At this Point I want something like this array

$cart = ([id] => ,[currency]=> ,[country]=> )

How anyone guide me how can I set up a constructor for this call,even if the properties are empty , I just want the key values for the array so that I can set its values like below

$cart->id = 1;
$cart->currency = EUR;
$cart->country= DE;

in this way it would be much easier to call in a new CART in this example... and then manipulate the class properties in order to save to database etc

6 Answers 6

5
class Cart
{
    private $id, $currency, $country;

    public function __construct($id = 1, $currency = 'EUR', $country = 'DE')
    {
        $this->id = $id;
        $this->currency = $currency;
        $this->country = $country;
    }
}

If you pass no arguments to your constructor, it'll inherit the defaults in the function argument spec.

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

4 Comments

thans man I think this is what I was searching for.. and I think this can be modified in this order. Imagine I have an array from the database with the columns. Can they be set all automatic
@MRR: This will not output the array when used with echo or print_r
@MRR: Since you have accepted this as the correct answer I will assume you didn't know what you were asking because this clearly does not return an array when echoed.
The key thing to understand here is what the person asking needs, not what he wants because the explanation is often unclear due to lack of knowledge in certain matter. Yes, the code provided doesn't return an array, it sets default values if no values are specified when creating the object. One can pass an array of values the same way and set defaults if the array passed was nonexistent.
2

You shouldn't return an array in the constructor. You should always return the reference to the cart. Just add a method to get at your data.

class Cart {
    public $id = 1;
    public $currency = 'EUR';
    public $country  = 'DE'

   public function getData() {
      return array(
          'id' => $this->id,
          'currency' => $this->currency,
          'country'  => $this->country
      );
   }
}

$cart = new Cart();
print_r( $cart->getData() ); //will print the array

//you can also get at the property
$cart->id = 1;  
$cart->currency = 'EUR';
$cart->country= 'DE';

Comments

1

You could pass the values as parameters to the constructor of Cart

Like this:

 function __construct( Class $var ) {
        $this->var = $var;
 }

Or did I misunderstood your question?

Comments

0

I think you want to use the magic method __toString()

Example from the manual:

<?php
// Declare a simple class
class TestClass
{
    public $foo;

    public function __construct($foo)
    {
        $this->foo = $foo;
    }

    public function __toString()
    {
        return $this->foo;
    }
}

$class = new TestClass('Hello');
echo $class;
?>

Will output: Hello.

Change the method to return your array.

Comments

0

Print_r can do that. Just specify $id, $currency and $country as property and print will show you something like:

Cart Object ( [id:Cart:private] => 1 
              [currency:Cart:private] => EUR 
              [country:Cart:private] => DE 
            )

So i don't get your question

Comments

0

The constructor returns a reference to the object instance just created. It cannot return anything different.

You could implement a method inside your Cart object "toArray()" that returns an associative array "attribute" => "value", to fit your needs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.