SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Something wrong when set public class

    I have two classes as below:

    product.php
    PHP Code:
    <?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 Code:
    <?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 Code:
    <?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"?

  2. #2
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=greedyman;5304602]
    When I put "public" for class Product, error occur and if I remove it, no error:

    PHP Code:
    <?php
        
    public class Product {
            
    //
        
    }
    ?>
    You cannot add "public" to class definitions in PHP, only to methods.

    Quote Originally Posted by greedyman View Post
    And why don't I add a construct function "Product" with one parameter "$id"?
    I don't really understand your question...

  3. #3
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    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

  4. #4
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    [QUOTE=Lemon Juice;5304611]
    Quote Originally Posted by greedyman View Post
    When I put "public" for class Product, error occur and if I remove it, no error:

    PHP Code:
    <?php
        
    public class Product {
            
    //
        
    }
    ?>
    You cannot add "public" to class definitions in PHP, only to methods.


    I don't really understand your question...
    Quote Originally Posted by TomB View Post
    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
    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?

  5. #5
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by greedyman View Post
    @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.
    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:
    PHP Code:
    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...
      
    }


  6. #6
    SitePoint Addict bronze trophy vectorialpx's Avatar
    Join Date
    Dec 2012
    Location
    Bucharest
    Posts
    247
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    That means that your method exists.

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

    PHP Code:
    <?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);
    ?>

  7. #7
    SitePoint Guru bronze trophy TomB's Avatar
    Join Date
    Oct 2005
    Location
    Milton Keynes, UK
    Posts
    988
    Mentioned
    9 Post(s)
    Tagged
    2 Thread(s)
    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:

    PHP Code:

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

    new 
    Test(12);
    new 
    Test('A''B''C'); 

  8. #8
    SitePoint Enthusiast greedyman's Avatar
    Join Date
    Jun 2012
    Location
    New World
    Posts
    80
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by vectorialpx View Post
    That means that your method exists.

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

    PHP Code:
    <?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);
    ?>
    Quote Originally Posted by Lemon Juice View Post
    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:
    PHP Code:
    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...
      
    }

    I just repaired it. Thanks you!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •