SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    assign variables in php classes

    Hi! I have a vary simple question, which I don't know how to solve. I'm a starter - so I want to know, how can I assign variable to a variable in a class - something like this:

    var $a = var $b;

    ?

  2. #2
    SitePoint Evangelist
    Join Date
    May 2006
    Location
    Austin
    Posts
    401
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    You can just do $a = $b;

    If you want to reference a variable you can use $a = &$b;

    If you are trying to copy a Class you can use clone.

    $a = new Class;

    $b = clone $a;
    Merchant Equipment Store - Merchant Services, POS, Equipment, and supplies.
    Merchant Account Blog | Ecommerce Blog

  3. #3
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, but it doesn't work, it just gives me an error:

    Parse error: syntax error, unexpected T_VARIABLE in ... on line 63, which is:
    $baseDir = $curr_gal_fold;

    Here's what I have in my code:

    $curr_gal_fold = strtr($_GET['n_g'], $replace);

    class maxImageUpload {
    var $maxUploadSize = 10;
    $baseDir= $curr_gal_fold; // here start's the problem
    ...
    }

  4. #4
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You need to set the value in the constructor, or define a setter method, if you want the value to be another variable. The default value must be a constant value or expression.

  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)
    How about like this?
    PHP Code:
    $curr_gal_fold strtr($_GET['n_g'], $replace);

    class 
    maxImageUpload {
        var 
    $maxUploadSize 10;
        var 
    $baseDir;
        
        function 
    __construct(){
            
    $this->baseDir $curr_gal_fold;
        }

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  6. #6
    SitePoint Addict
    Join Date
    Jul 2007
    Location
    San Jose, California
    Posts
    355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The class doesn't ahve access to that variable.

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    $curr_gal_fold 
    strtr($_GET['n_g'], $replace);

    class 
    maxImageUpload {
        var 
    $maxUploadSize 10;
        var 
    $baseDir;
        
        function 
    __construct($base_dir){
            
    $this->baseDir $base_dir;
        }


    $oUpload = new maxImageUpload($curr_gal_fold)
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by rajug View Post
    How about like this?
    PHP Code:
    $curr_gal_fold strtr($_GET['n_g'], $replace);

    class 
    maxImageUpload {
        var 
    $maxUploadSize 10;
        var 
    $baseDir;
        
        function 
    __construct(){
            
    $this->baseDir $curr_gal_fold;
        }

    Are you sure, that this will work, because I'm trying, but all it gives, is a blank $basedir variable. Perhaps I'm doing something wrong?

  9. #9
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Take a quick peek at my post (#7).

    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  10. #10
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, that didn't work for me either - I must be doing something wrong, because of my weak knowledge of oop. Maybe you could help me - the whole code is here:

    maximageupload.class.php
    (lines 63, 85)

    And basically I'm just calling these 2 functions in another file:

    PHP Code:
       require_once("general/maximageupload.class.php");
    $myImageUpload = new maxImageUpload($folderis);
    $myImageUpload->uploadImage(); 
    Great if you could give me some advice

  11. #11
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your $folderis variable needs a value in the current scope.

    For example
    PHP Code:
    $folderis strtr($_GET['n_g'], $replace);
    $myImageUpload = new maxImageUpload($folderis); 

  12. #12
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Already tried that - the result was the same. In general this script, should create/update a directory basing on a $baseDir variable. As things stand, no directory is created (however the files are stored at the constant script base folder), therefore it seems that the $baseDir is not getting my variable.

  13. #13
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do some basic debugging. Use var_dump() on variables at strategic points in your script to isolate issues. You're making a lot of assumptions about which variables have what values. Check your assumptions.

  14. #14
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Offtopic:
    Could, should or can this kind off image upload class be singleton ?

  15. #15
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by crmalibu View Post
    Do some basic debugging. Use var_dump() on variables at strategic points in your script to isolate issues. You're making a lot of assumptions about which variables have what values. Check your assumptions.
    Doing a var_dump($folderis) and var_dump($myImageUpload) in the 1st file where I call the functions out, it gives me everything:

    object(maxImageUpload)#1 (17) { ["maxUploadSize"]=> int(10) ["normalWidth"]=> int(480) ["normalHeight"]=> int(360) ["thumbWidth"]=> int(96) ["thumbHeight"]=> int(72) ["imageQualityNormal"]=> int(3) ["imageQualityThumb"]=> int(3) ["baseDir"]=> string(11) "some_folder" ["originalDir"]=> string(8) "original" ["normalDir"]=> string(6) "normal" ["thumbDir"]=> string(9) "thumbnail" ["infoDir"]=> string(4) "info" ["originalPrefix"]=> string(0) "" ["normalPrefix"]=> string(7) "normal_" ["thumbPrefix"]=> string(6) "thumb_" ["error"]=> string(0) "" ["maxMemoryUsage"]=> int(128) }

    however, in the second file (maximageupload.class.php), var_dump($baseDir); leads to NULL - I don't understand it.

    Doesn't seem to be a problem here:

    function __construct($base_dir){
    $this->baseDir = $base_dir;
    }

  16. #16
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    class maxImageUpload {
        var 
    $maxUploadSize 10;
        var 
    $baseDir;
        
        function 
    __construct($base_dir){
            
    $this->baseDir $base_dir;
        }

        function 
    uploadImage() {
            
    var_dump($baseDir); // null, php thinks you're asking for a "local variable"
            
    var_dump($this->baseDir); // works. php knows you want an "instance variable"
        
    }


  17. #17
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So ok, now, when I have the baseDir with the correct value, everything should work, but something's missing. Unfortunately it doesn't create any directories at all, as is written in the maximageupload.class.php scipt. However, if I go back to the start and simply change:

    $myImageUpload = new maxImageUpload($folderis); -> $myImageUpload = new maxImageUpload();

    var $baseDir -> var $baseDir='some_folder';

    and delete this:

    Code:
    	 function __construct($base_dir){
         $this->baseDir = $base_dir;
        }
    it creates all the directories I needed. I understand that it may sound stupid to compare two of these variants, but I see no solution, because var_dump($myImageUpload->baseDir); in the 1st file or var_dump(baseDir); in the second gives me absolutely identical values of the baseDir - the only difference is that in the 1st case it doesn't create folders, but in the second, it does. Any ideas, where can my error be?

  18. #18
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not without you providing any code.

  19. #19
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here's the 1st file called. admin.php and the 2nd file called maximageupload.class.php

  20. #20
    SitePoint Wizard bronze trophy
    Join Date
    Jul 2008
    Posts
    5,757
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    http://www.php.net/manual/en/language.oop5.decon.php

    when php sees __construct(), it calls that function when you instantiate a new object. For backwards compatibility, php still looks for the older style constructor if it cannot find __construct(). The name of the older style constructor is the same as the name of the class.

    So, basically, you need to manually call your maxImageUpload() method. You should also rename it to improve the clarity of your code.

  21. #21
    SitePoint Addict
    Join Date
    May 2006
    Posts
    236
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Wow, I was thinking about this earlier, but never tried this thing. Million thanks, you're a real SitePoint wizard!!!


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
  •