Something wrong when set public class

I have two classes as below:

product.php


<?php
	class Product {
		private $id;
		private $name;
		private $price;
		private $lastUpdate;
		private $category;
		
		//public function Product($id) {
		//	$this->id = $id;
		//}
		
		public function Product($id, $name, $price, $lastUpdate) {
			$this->id = $id;
			$this->name = $name;
			$this->price = $price;
			$this->lastUpdate = $lastUpdate;
		}
		
		public function getId() {
			return $this->id;
		}
		
		public function setId($id) {
			$this->id = $id;
		}
		
		public function getName() {
			return $this->name;
		}
		
		public function setName($name) {
			$this->name = $name;
		}
		
		public function getPrice() {
			return $this->price;
		}
		
		public function setPrice($price) {
			$this->price = $price;
		}
		
		public function getLastUpdate() {
			return $this->lastUpdate;
		}
		
		public function setLastUpdate($lastUpdate) {
			$this->lastUpdate = $lastUpdate;
		}
		
		public function getCategory() {
			return $this->category;
		}
		
		public function setCategory($category) {
			$this->category = $category;
		}
	}
?>

index.php


<?php
	include('product.php');
	include('ShoppingCartItem.php');
	//include('ShoppingCart.php');
	
	$product = new Product(1, 'Note book', 25000, '25/1/2013');
	//echo $product->getId();
	$scItem = new ShoppingCartItem($product);
	
	//echo $scItem->getQuantity();
?>

When I put “public” for class Product, error occur and if I remove it, no error:


<?php
	public class Product {
		//
	}
?>

Parse error: syntax error, unexpected T_PUBLIC in C:\xampp\htdocs\demo\passing_object_to_function\product.php on line 2

Why don’t I do this? And why don’t I add a construct function “Product” with one parameter “$id”?

[quote=“greedyman”]

When I put “public” for class Product, error occur and if I remove it, no error:


<?php
	public class Product {
		//
	}
?>

You cannot add “public” to class definitions in PHP, only to methods.

I don’t really understand your question…

As Lemon Juice said, classes are by definition public and cannot be stated otherwise. Remove the public keyword and it will fix the issue.

However, it should be noted that __construct() is preferred to using the class’ name as the constructor. See: http://php.net/manual/en/language.oop5.decon.php

[quote=“Lemon_Juice,post:2,topic:26287”]

Thanks! I see.
@Lemon Juice: Do you see a paragraph I marked by orange color in Product class? As Java or C#, I can make a lot of construct function with one, two, …etc parameter(s) but in PHP, I can’t. When I try to do this, I see an error:

Fatal error: Cannot redeclare Product::Product() in C:\xampp\htdocs\demo\passing_object_to_function\product.php on line 13

This is thing I don’t understand?

I see, you mean method overloading. It’s not possible in PHP, the closest you can get to it is accept optional parameters and then use if statements to do different things:


public function Product($id, $name = null, $price = null, $lastUpdate = null) {
  if ($name === null && $price === null && $lastUpdate === null) {
    // do something when only $id is given...
  } else {
    // do something else...
  }
}

That means that your method exists.

About overloading, you may use __call but not for the constructor


<?php

class test {

	function some1($a) {
		echo '1 => '.$a;
	}
	
	function some2($a, $b) {
		echo '2 => '.$a.', '.$b;
	}
	
	public function __call($method, $params) {
		if(count($params) == 2) {
			$this->some2($params[0], $params[1]);
		} elseif(count($params) == 1) {
			$this->some1($params[0]);
		}
	}

}

$a = 'a';
$b = 'b';
$test = new test;
$test->some($a);
echo '<br />----<br />';
$test->some($a, $b);
?>

actually you can use func_get_args() and manually code the branching, but it’s a bit hacky and makes it very difficult for people using the class to see what is needed by the constructor:



class Test {
	public function __construct() {
		print_r(func_get_args());
	}
}

new Test(1, 2);
new Test('A', 'B', 'C');

I just repaired it. Thanks you!