Problems with own DA API class (PHP)

Ywa

Verified User
Joined
Feb 4, 2010
Messages
37
Location
The Netherlands
Hi guys,

I'm trying to create my own PHP class for the DirectAdmin API. However, it doesn't seem to work at all. Does anyone know what's causing it and how it can be fixed?

Script code: Pastebin
All variables with the da_ prefix are defined in config.php and are right.

There are no errors but the script returns this:
Code:
CreateConnection
Error Creating user pizza on server :


CreateUser
da_api

Thanks in advance!
 
Last edited:
You cannot directly refer to global variables in PHP functions or methods, meaning all variables prefixed by "da_" cannot be accessed within the method scope. Turn on PHP notices, you'll get lots of messages saying that there is no such variable available.

There are a few possible solutions, best one would be to create an array out of the DirectAdmin configuration and pass it to the class using a constructor. That would work like this;

Config.php
Code:
<?php
$aDirectAdminSettings = array
(
        'hostname'  => '127.0.0.1',
        'port' => 2222
);

Class.php
Code:
<?php
class DirectAdminAPI
{
        private $m_aConfiguration;
        public function __construct ($aConfiguration)
        {
                $this -> m_aConfiguration = $aConfiguration;
        }
        public function randomMethod ()
        {
                echo $this -> m_aConfiguration ['hostname'];
        }
}

$pDirectAdmin = new DirectAdminAPI ($aDirectAdminSettings);

Alternatively, the more ugly way would be to use the "global" keyword in your class' method. This indicates that you wish to use a global variable in a local scope.

More information on PHP's variable scope can be found here: http://nl3.php.net/manual/en/language.variables.scope.php

Good luck,
Peter
 
Back
Top