Asked  7 Months ago    Answers:  5   Viewed   39 times

Let's say I've got two files class.php and page.php

class.php

<?php 
    class IUarts {
        function __construct() {
            $this->data = get_data('mydata');
        }
    }
?>

That's a very rudamentary example, but let's say I want to use:

$vars = new IUarts(); 
print($vars->data);

in my page.php file; how do I go about doing that? If I do include(LIB.'/class.php'); it yells at me and gives me Fatal error: Cannot redeclare class IUarts in /dir/class.php on line 4

 Answers

18

You can use include/include_once or require/require_once

require_once('class.php');

Alternatively, use autoloading by adding to page.php

<?php 
function my_autoloader($class) {
    include 'classes/' . $class . '.class.php';
}

spl_autoload_register('my_autoloader');

$vars = new IUarts(); 
print($vars->data);    
?>

It also works adding that __autoload function in a lib that you include on every file like utils.php.

There is also this post that has a nice and different approach.

Efficient PHP auto-loading and naming strategies

Wednesday, March 31, 2021
 
Angolao
answered 7 Months ago
86

You should point to your vendor/autoload.php at Settings | PHP | PHPUnit when using PHPUnit via Composer.

This blog post has all the details (with pictures) to successfully configure IDE for such scenario: http://confluence.jetbrains.com/display/PhpStorm/PHPUnit+Installation+via+Composer+in+PhpStorm

Related usability ticket: http://youtrack.jetbrains.com/issue/WI-18388

P.S. The WI-18388 ticket is already fixed in v8.0

Wednesday, March 31, 2021
 
ojrac
answered 7 Months ago
30

If both of your function are residing in the same class you can call a function inside another function using $this as pointed in other answers

class myclass{

public $var1 = 121;
    public function foo($postid){
        echo $postid;
    }
    public function bar(){
        $this->foo($this->var1);
    }
}
$objectmyclass = new myclass();
$objectmyclass->bar();

OR

class myclass{

public $var1 = 122;
    public function foo($postid){
        echo $postid;
    }
    public function bar($postid){
        $this->foo($postid);
    }
}
$objectmyclass = new myclass();
$objectmyclass->bar($objectmyclass->var1);

both ways you will be able to execute the statements inside the function foo hope this helps

Wednesday, March 31, 2021
 
Blur
answered 7 Months ago
79

On Mac OS X environment variables available in Terminal and for the normal applications can be different, check the related question for the solution how to make them similar.

Note that this solution will not work on Mountain Lion (10.8).

Saturday, May 29, 2021
 
Nate
answered 5 Months ago
46

I think you're approaching this wrong. I would suggest you create a database table called config and have in here columns for APIKey, USERNAME and PASSWORD. Within your admin.php you can allow the user to update these records ..

update settings_table set APIKEY='a', USERNAME='b', PASSWORD='c' where id=1

And then on your front end you can query this table to get the values ..

$query = mysql_query("select * from settings_table where id=1", $connection);
$row = mysql_fetch_array($query, MYSQL_ASSOC);

$CONFIG = Array (
   'APIKey' => $row['APIKey'],
   'USERNAME' => $row['USERNAME'],
   'PASSWORD' => $row['PASSWORD'],
);

EDIT: You don't want to use MySQL

OK, you could do this with a file that stores the config settings. You should put this into a non-web accessible directory.

So, in your admin.php file you can write the details to a file:

$myFile = "/full/path/to/config.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = $_POST['APIKey'] . "n";
$stringData = $_POST['USERNAME'] . "n";
$stringData = $_POST['PASSWORD'] . "n";
fclose($fh);

And then on your front end you can open the file, split up the rows and then build your $CONFIG array:

$data = file_get_contents("/full/path/to/config.txt");
$data = explode("n", $data);

$CONFIG = Array (
   'APIKey' => $data[0],
   'USERNAME' => $data[1],
   'PASSWORD' => $data[2],
);
Saturday, May 29, 2021
 
phpmeh
answered 5 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :