Solved

how to point the paremeter variable from config to class

Posted on 2008-06-19
18
226 Views
Last Modified: 2008-06-20
PHP

How can i point the parameter variable in my config file to a class

i have a class call TEST class
i also have a main.php where i use to create object of class
i have also config.php --> use to ease end user to change the setting

now in my config file i have this


**config.php***
$localFolder = "c:/TEST";    
$host = "hostname";        
$user = "username";            
$pass = "password";  
$remoteFolder = '/JOHN/TEST';  
$this->uploadFiles($localFolder, $host, $user, $pass, $remoteFolder);

in my class i have this

***TEST.php***
class Test
{
....
function uploadFiles($localFolder, $host, $user, $pass, $remoteFolder)
{}
....
}

in my main i have

****main.php***
include "Test.php";
$a = new Test();
$a->otherfunction();

Since i had create object Test class i cannot create the same object class in config file
that mean i cannot do this

****config.php***
include "Test.php";
$b = new Test();
$b->uploadFiles($localFolder, $host, $user, $pass, $remoteFolder);

if i cannot use the method above i cannot pass the parameter to the class.

another method i use is using $this->
but using $this-> it keep looping without end.

what is the proper way to do this




0
Comment
Question by:firekiller15
  • 9
  • 9
18 Comments
 
LVL 49

Expert Comment

by:Roonaan
ID: 21820407
Hi,

You pass parameters at the exact moment when you call the function.

You could just use

$a = new Test();
$a->uploadFiles('c:/test1', 'server', 'user', 'password', '/www/test1');
$a->uploadFiles('c:/test2', 'server', 'user', 'password', '/www/test2');
$a->uploadFiles('c:/test3', 'server', 'user', 'password', '/www/test3');
0
 

Author Comment

by:firekiller15
ID: 21827932
if i put this in
$a = new Test();
$a->uploadFiles('c:/test1', 'server', 'user', 'password', '/www/test1');
how can i point the value from config to main?

i cannot put the value in the parameter because it does not ease end user to change the setting in the future
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21828568
In config you would have your properties:
$host = 'something';
$user = 'something';
$password = 'something';
$localFolder1 = 'something';
$remoteFolder1 = 'something';
$localFolder2 = 'something';
$remoteFolder2 = 'something';

then have

$a = new Test();
$a->uploadFiles($localFolder1, $host, $user, $password, $remoteFolder1);
$a->uploadFiles($localFolder2, $host, $user, $password, $remoteFolder2);
0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 

Author Comment

by:firekiller15
ID: 21828668
i dont think can do like this
i had try to do as follow
i call by include("config.php"); in main
i received this error message
Fatal error: Using $this when not in object context in C:\wamp\www\test\config.php on line 49
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21828684
That is correct. You are not in a object in the config, as it is a config and not execution.

Remote the $this->uploadFiles($localFolder, $host, $user, $pass, $remoteFolder); line and use the $a = new Test() stuff from later.
0
 

Author Comment

by:firekiller15
ID: 21828757
you mean i place this Remote the $this->uploadFiles($localFolder, $host, $user, $pass, $remoteFolder); in config file?

i try this also..
i can get the result but
i get the result again and again its like never ending
looks like im looping and looping the data.
also in my config file i have other value.
but if i use $this-> other data cannot be execute by other function
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21828777
No.

Config file holds config only!
[config.php]
$host = 'something';
$user = 'something';
$password = 'something';
$localFolder1 = 'something';
$remoteFolder1 = 'something';
$localFolder2 = 'something';
$remoteFolder2 = 'something';

Your main file holds the calls to the upload functions:

[main.php]
$a = new Test();
$a->uploadFiles($localFolder1, $host, $user, $password, $remoteFolder1);
$a->uploadFiles($localFolder2, $host, $user, $password, $remoteFolder2);

0
 

Author Comment

by:firekiller15
ID: 21828865
so you mean
Config file holds config only!
[config.php]
$host = 'something';
$user = 'something';
$password = 'something';
$localFolder1 = 'something';
$remoteFolder1 = 'something';
$localFolder2 = 'something';
$remoteFolder2 = 'something';

[main.php]
include("config.php");
$a = new Test();
 $this->uploadFiles($localFolder, $host, $user, $pass, $remoteFolder);

??
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21828876
Yeah, otherwise I wouldn't have typed it.
0
 

Author Comment

by:firekiller15
ID: 21828973
you type
$a->uploadFiles($localFolder2, $host, $user, $password, $remoteFolder2);

is $a or $this??
0
 

Author Comment

by:firekiller15
ID: 21828975
if u mean $this cannot received

Fatal error: Using $this when not in object context in C:\wamp\www\ERI\config.php on line 49

if u mean $a i already post above why cannot use $a
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21828987
You can use $this only INSIDE an object. As you have not an object yet, you need to instantiate one. That is why you NEED to use $a.
I don't understand why you think $a cannot be used. Configurability is not an issue when using $a as I explained.
0
 

Author Comment

by:firekiller15
ID: 21828994
furthermore if u want to put $a->uploadFiles($localFolder2, $host, $user, $password, $remoteFolder2);
this in main.php is imposible

because you need to call the value from config.php and that mean you need to include config.php in main
. this will cause an errror of this
Fatal error: Using $this when not in object context in C:\wamp\www\test\config.php on line 49
0
 

Author Comment

by:firekiller15
ID: 21828996
do you mind to show the detail of the code how to do this ?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21829000
I posted it already:

[config.php]
$host = 'something';
$user = 'something';
$password = 'something';
$localFolder1 = 'something';
$remoteFolder1 = 'something';
$localFolder2 = 'something';
$remoteFolder2 = 'something';

Your main file holds the calls to the upload functions:

[main.php]
$a = new Test();
$a->uploadFiles($localFolder1, $host, $user, $password, $remoteFolder1);
$a->uploadFiles($localFolder2, $host, $user, $password, $remoteFolder2);

Can you explain why this does not work for you?
0
 
LVL 49

Expert Comment

by:Roonaan
ID: 21829006
For main.php you would indeed have to do some includes:


[main.php]
<?php
include 'config.php';
include 'Test.php';

$a = new Test();
$a->uploadFiles($localFolder1, $host, $user, $password, $remoteFolder1);
$a->uploadFiles($localFolder2, $host, $user, $password, $remoteFolder2);
0
 

Author Comment

by:firekiller15
ID: 21829113
cannot i do as what you state above i received this error message
Fatal error: Using $this when not in object context in C:\wamp\www\sample\config.php on line 49

just that instead of this two
$a->uploadFiles($localFolder1, $host, $user, $password, $remoteFolder1);
$a->uploadFiles($localFolder2, $host, $user, $password, $remoteFolder2);

i use only
$a->uploadFiles($localFolder1, $host, $user, $password, $remoteFolder1);
0
 
LVL 49

Accepted Solution

by:
Roonaan earned 250 total points
ID: 21829144
You would remove the $this->uploadFiles from the config file line 49, as you use $a instead.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question