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;
?
| SitePoint Sponsor |


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


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;


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
...
}
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.
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;
}
}


The class doesn't ahve access to that variable.
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.


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.


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:
Great if you could give me some advicePHP Code:require_once("general/maximageupload.class.php");
$myImageUpload = new maxImageUpload($folderis);
$myImageUpload->uploadImage();
![]()
Your $folderis variable needs a value in the current scope.
For example
PHP Code:$folderis = strtr($_GET['n_g'], $replace);
$myImageUpload = new maxImageUpload($folderis);


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.
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.


Offtopic:
Could, should or can this kind off image upload class be singleton ?


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;
}
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"
}
}


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:
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?Code:function __construct($base_dir){ $this->baseDir = $base_dir; }
Not without you providing any code.


Here's the 1st file called. admin.php and the 2nd file called maximageupload.class.php
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.


Wow, I was thinking about this earlier, but never tried this thing. Million thanks, you're a real SitePoint wizard!!!![]()
Bookmarks