Solved

how to point the paremeter variable from config to class

Posted on 2008-06-19
18
217 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 

Author Comment

by:firekiller15
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Yeah, otherwise I wouldn't have typed it.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

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

is $a or $this??
0
 

Author Comment

by:firekiller15
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
do you mind to show the detail of the code how to do this ?
0
 
LVL 49

Expert Comment

by:Roonaan
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You would remove the $this->uploadFiles from the config file line 49, as you use $a instead.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
This article discusses how to create an extensible mechanism for linked drop downs.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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.

771 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now