BoqBoq
BoqBoq

Reputation: 4704

Class Constructor PHP Help

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

Upvotes: 1

Views: 779

Answers (6)

JohnP
JohnP

Reputation: 50019

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';

Upvotes: 2

Fabrizio D'Ammassa
Fabrizio D'Ammassa

Reputation: 4769

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.

Upvotes: 0

dynamic
dynamic

Reputation: 48101

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

Upvotes: 0

AntonioCS
AntonioCS

Reputation: 8496

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.

Upvotes: 0

Michael J.V.
Michael J.V.

Reputation: 5609

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.

Upvotes: 5

Tobias
Tobias

Reputation: 7380

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?

Upvotes: 1

Related Questions