1

To begin, here is the flow so you can understand the problem. I have four files, three of these are classes, I use two namespaces.
login.php is a form, when the form is submited, it comes back to itself and the code below is executed. The login.php calls the Zcrypt::Decrypt and Zcrypt::Encrypt with out issues. the Login::DoLogin(); is also called inside the login.php file.

In the Login.class.php (where DoLogin lives) file I create a new instance of DB, and can call Zcrypt::Decrypt with out error. In Login.class.php I call dbConnect();

In the DB.class.php (where dbConnect lives) file I am unable to call Zcrypt::Decrypt. It gives me a syntax error or that it can not find Zcrypt. I have tried Zcrypt::Decrypt([string]), \Zcrypt::Decrypt([string]), and even \Zcrypt::Decrypt([string]).

The question is, how come I can call Zcrypt in some classes but not others? Im I missing some code for this to work?

Here are my files

login.php:

require 'NS/helpdesk/Login.class.php';
require 'NS/helpdesk/Cryptv2.class.php';
require 'NS/helpdesk/DB.class.php';
use \net\[domain]\Zcrypt;
use \net\[domain]\helpdesk\Login;

#check to see if the form was submited and that the values are equal.
{
if (strlen($_POST['hvalue']) > 1 && $_SERVER['REMOTE_ADDR'] == Zcrypt::Decrypt($_POST['hvalue']) )
{
        Login::DoLogin();  ###### This is where I call my static Login Class
}
else {
    echo "bad form";
}
}

Login.class.php

namespace net\[domain]\helpdesk;

use \net\[domain]\helpdesk\DB;
use \net\[domain]\Zcrypt;
class Login
{

    public function DoLogin()
    {
        #call to the database class to open the db
        $DB = new DB();
        $DB->dbConnect();

        #This is to show I can call Zcrypt in this class (note, no \) and it works.     
        echo $dbPass = Zcrypt::Decrypt("[coded string]");   
    }
}

DB.class.php

namespace net\[domain]\helpdesk;

use \net\[domain]\Zcrypt;

class DB
{

    public $dbHost = '[address]';
    public $dbUser = '[un]';
    public $dbPass = '[pw]';  
  ######The two commented out lines below will not run.  I get a syntax error
    # public $dbPass = \Zcrypt::Decrypt("[strint]"); 
    # public $dbPass = Zcrypt::Decrypt("[string]")                   
    public $dbName = '[name]';
    public $db;

    public function __construct(){}

    public function dbConnect()
    {    
        [code]
    }
}

Cryptv2.class.php

namespace net\[domain];

use Exception;


class Zcrypt
{
    public static function Encrypt($i)
    {
                [code]  
    }
    public static function Decrypt($i)
    {
        [code]
    }
}

Thanks for your help.

1
  • In DB.class.php at line 13, If I uncomment this line, it finds Zcrypt, however, I get a syntax error on the ( when I run it. Why? Commented Nov 1, 2013 at 2:13

2 Answers 2

1

It's syntax error. You cannot use expressions in property definition.

This declaration may include an initialization, but this initialization must be a constant value--that is, it must be able to be evaluated at compile time and must not depend on run-time information in order to be evaluated.

http://php.net/manual/en/language.oop5.properties.php

Sign up to request clarification or add additional context in comments.

1 Comment

I made db.class.php look like this<br> ' namespace net[domain]\helpdesk; use \net[domain]\Zcrypt; class DB { public $dbHost = '[address]'; public $dbUser = '[un]'; public $dbPass = ''; public $dbName = '[name]'; public $db; public function __construct(){} public function dbConnect() { #redefine $dbPass in this function $this->dbPass = Zcrypt::Dectrypt('[string]'); [code] } } ' Thank you for your help.
0

You would have to use \net\[domain]\Zcrypt:: for this to work. Or better yet assign an alias like use \net\[domain] as z, then z\Zcrypt::. In other words see the PHP manual http://php.net/manual/en/language.namespaces.php . Find file4. It has the example you need.

5 Comments

I have \net[domain] on the Zcrypt page as Zcrypt is the class, not the name space. If you note in the Login.class.php page, I can call Zcrypt:: and it works fine, why is this?
I found that if I use \Zcrypt:: it will find the namespace, however, I get an error on the (, like it cannot find the function.
It is required in login.php, if I put a require in each class, I get an error that I am defining duplicate classes. require is nothing more than an include so basically when the login.php page runs, all of the four pages of code are in one, thus, only one require is needed. Right?
You have use \net\[domain]\Zcrypt; should be use \net\[domain];.
I thought that when you call the namespace it is then followed with the class name since PHP does not support * [namespace][class] \net[domain]\Zcrypt;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.