SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict dr_dumb99's Avatar
    Join Date
    Sep 2004
    Location
    Pakistan
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question How to assign value to variable in Classes

    Hi!
    I have following class:

    class Tbl {

    var $tbluser = DB_PREFIX.'user';


    } // END OF CLASS



    NOTE : DB_PREFIX is defined variable with a value of "CUT" ;

    When I call this class, I get following PHP error message :

    /************************************/
    syntax error, unexpected '.', expecting ',' or ';' in E:\personal\humor\base\classes\tbl.php

    /***********************************/


    2- How to define an array type properties and assign them values in PHP classes ?

    Thanks in advance

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not sure whether i got what your problem is but try out like this:
    Code php:
    class Tbl{
    	var $tbluser;
    	function __construct($dbprefix){
    		$this->tbluser = $dbprefix . "user";
    	}
    }
    $obj = new Tbl("DB_PREFIX");
    Last edited by Raju Gautam; Aug 1, 2007 at 04:24.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  3. #3
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    I am not sure whether i got what your problem is but try out like this:
    Code php:
    class Tbl{
        var $tbluser;
        function __construct($dbprefix){
            $tbluser = $dbprefix . "user";
        }
    }
    $obj = new Tbl("DB_PREFIX");
    I think you mean
    PHP Code:
    $this->tbluser $dbprefix "user"
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  4. #4
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can't concatenate, or use any functions when assigning values to member variables in the object.

    You need to use the constructor for that. Because DBPREFIX is a constant you don't need to take it as an argument to your constructor. (though you could if you wanted to)

    PHP Code:
    class Tbl {
       private 
    $tbluser;

       public function 
    __construct() {
          
    $this->tbluser DBPREFIX 'user';
       }

    Btw, using the var word is so PHP4. Use private|protected|public instead

  5. #5
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    I think you mean
    PHP Code:
    $this->tbluser $dbprefix "user"
    Yes logic_earth. Sorry i was little mistake.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Addict dr_dumb99's Avatar
    Join Date
    Sep 2004
    Location
    Pakistan
    Posts
    304
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your help.

    How can I initialize array and assign values in a class.

  7. #7
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    See this:
    Code php:
    class Tbl {
    	private $tbluser;
    	var $myArr = array();
    	public function __construct() {
    		$this->tbluser = DBPREFIX . 'user';
    		$this->myArr[] = "SomeValues";
    	}
    }
    #But if you want to assign values outside class then you create the object and assign the values likewise:
    $obj = new Tbl();
    $obj->myArr[] = "Somevalue";
    print_r($obj->myArr);
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


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
  •