开发者

OOP PHP - get's no value in variables

开发者 https://www.devze.com 2023-04-05 13:28 出处:网络
I\'m kinda confused, I\'m new to OOP PHP, so can\'t stop my function get empty values. I have an index.php file where I have:

I'm kinda confused, I'm new to OOP PHP, so can't stop my function get empty values.

I have an index.php file where I have:

<?php   
require_once('../../includes/functions.php'); 
require_once('../../includes/database.php');
require_once('../../includes/user.php'); 


$user = new User();
$user->username = 'test';
$user->password = '1234';
$user->firstname = 'test1';
$user->lastname = 'test2';

User::create();

// $user->traceStatement();


?>

I have a User class that extends Databaseobject class: user.php:

<?php 
require_once('database.php');

class User extends DatabaseObject {

    protected static $tableName = 'users';
    protected static $tableID = 'id';

    public $id;
    public $username;   
    public $password;   
    public $firstname;
    public $lastname;

    public function traceStatement() {
        echo $this->username;   
        echo $this->password;   
        echo $this->firstname;  
        echo $this->lastname;                                   
    }


}

?>

and here's a snippet of create function from DatabaseObject class:

public function create() {
    global $database;
    $calledClass = get_called_class();
    $class = new $calledClass;      
    $sql = "INSERT INTO ".$calledClass::$tableName." (username, password, firstname, lastname) VALUES ('";
    $sql .= $database->escapeValue($class->username)."', '";
    $sql .= $database->escapeValue($class->password)."', '";
    $sql .= $database->escapeValue($class->firstname)."', '";
    $sql .= $database->escapeValue($class->lastname);
    $sql .= "')";   
    if($database->query($sql)) {
        $cass->id = $database->insert_id();
        return true;
    }else {
        return false;
    }
}

As far as I understand the idea is that

on my index.php file i'm declaring varibales of User class, these info are sent to user.php and I'm also calling create() function from databaseobject class through Us开发者_高级运维er:: class, and my info inside variables will be sent to create function.

in the database I'm getting blank fields.

I tried to hardcode the variables inside my user.php - and everything work (first I thought I wasn't getting data from user.php).

When it worked I thought maybe I'm not gettind data from index.php to user.php but it turned out that when I wrote function traceStatement() - I got back the data in index.php file...

So maybe someone here know what's my problem, what am I doing wrong?


Rather thanUser::create();, you should use $user->create().

And your create() method should change to: ($this is very important in the OOP world)

public function create() {
    global $database;
    $sql = "INSERT INTO ".static::$tableName." (username, password, firstname, lastname) VALUES ('";
    $sql .= $database->escapeValue($this->username)."', '";
    $sql .= $database->escapeValue($this->password)."', '";
    $sql .= $database->escapeValue($this->firstname)."', '";
    $sql .= $database->escapeValue($this->lastname);
    $sql .= "')";   
    if($database->query($sql)) {
        $this->id = $database->insert_id();
        return true;
    }else {
        return false;
    }

}


DataBaseObject.create(0 function contains: $class = new $calledClass;

This will create a new User object, which hasn't been initialized. ==> empty values.

In index.php, you should call:

$user->create();

(instead of User::create() ==> static)

In the databaseObject, you can just use $this->tableName, .. instead of all the $calledClass stuff. I don't see why you want this to be static.


The problem is you're calling the create() method from a static context. If you want the properties to be available in the method, you need to call it like this: $User->create().

0

精彩评论

暂无评论...
验证码 换一张
取 消