How to instantiate a class in another class

Maybe something is missing me, I'm not sure. Google search also did not help.

What I want to do is call the databaseServer class and use its methods in my userControl class. Here is my lib_class.php file:

<?php

include('definitions.php');

class databaseServer {

    var $con;
    var $db;
    var $close;
    var $qry;
    var $sql;

    function connect($host,$user,$pw,$db) {
        $this->con = mysql_connect($host,$user,$pw);
        if (!$this->con) {
            die('Could not connect: ' . mysql_error());
            }
        else {
            echo "Database Connected";
            }
        $this->selectDb($db);
        }

    function selectDb($database) {
        $this->db = mysql_select_db($database,$this->con);
        if (!$this->db) {
            echo "Could not Select database";
            }
        else {
            echo "Database Selected";
            }
        }

    function disconnect() {
        $this->close = mysql_close($this->con);
        if ($this->close) {
            echo "Disconnected";
            }
        }

    function query($test) {
        if (!mysql_query($test)) {
            die("Error: " . mysql_error());
            }
        }

} // databaseServer

class cookie {

    var $expireTime;

    function set($name,$value,$expiry) {
        $this->expireTime = time()+60*60*24*$expiry;
        setcookie($name,$value,$expireTime);
        }

    function delete($name) {
        setcookie($name,"",time()-3600);
        }

    function check($name) {
        if (isset($_COOKIE["$name"]))
            echo "Cookie Set";
        else
            echo "Cookie failed";
        }

} //cookie

class userControl {

    public function __construct(databaseServer $server) {
        $this->server = new databaseServer();
    }

    function createUser($uname,$pword) {

        $this->server->connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
        $result = $this->server->query("SELECT * FROM user_list WHERE uname='" . $this->server->real_escape_string($uname) . "'");
        if ($this->result->num_rows() === 0) {

            if ($this->server->query("INSERT INTO user_list (uname, pword) 
            VALUES ('" . $this->server->real_escape_string($uname) . "','" . $this->server->real_escape_string($pword) . "')") {
                echo "User Added Successfully!";
            }
            else {
                echo "Error Adding User!";
            }
        }

        else {
            echo "User Already Exists!";
        }

    } // createUser

} // userControl

?>

However, this does not work, and I do not understand why. My databaseServer and cookie classes work fine when I omit the userControl class from a file, so I know that the error should be in this class somewhere. OOP is what I'm trying to learn, and I keep stumbling.

The echoes in the databaseServer class exist only for verification. I implement the classes in the index.php file as follows:

<?php

include('definitions.php');
include('class_lib.php');

$bmazed = new databaseServer();

$bmazed->connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);

$sql = "INSERT INTO blah
VALUES ('testing 92')";

$bmazed->query($sql);

$bmazed->disconnect();

// $control = new userControl();

// $uname = "Test1";
// $pword = "test1";

// $control->createUser($uname,$pword);

echo "<br />";
echo "<br />";

?>

The lines were commented out for testing purposes, so I do not need to continue to rewrite the code.

, , , .

+3
4

$server :

class userControl {

 private $server;

 function __construct() {
  $this->server = new databaseServer();
 }

 function createUser($uname,$pword) {
  $this->server->connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
  $result = $this->server->query("SELECT * FROM user_list WHERE uname='" . $this->server->real_escape_string($uname) . "'");
  if ($this->result->num_rows() === 0) {

   if ($this->server->query("INSERT INTO user_list (uname, pword) VALUES ( '" . $this->server->real_escape_string($uname) . "','" . $this->server->real_escape_string($pword) . "')") {
    echo "User added Succesfully";
    }
   else {
    echo "Error Adding User";
    }

  else {
   echo "User already exists";
   }
 }

}
+2

, . . PHP.

:

class userControl
{
    protected $_server;

    public function __construct ()
    {
        $this->_server = new databaseServer();
    }
}

, / $this, .

$this->_server->connect();

, composition , aggregation . , , , , UnitTests. Injection Dependency.

+6

-, $server createUser(), . PHP , C-.

$createOser(), createUser(), , , getServer(), .

, , , " $server" . . .

, , , COUNT (*), * SQL-, .:)

PHP, . (): http://php.net/manual/en/language.variables.scope.php

, !

+1

, , . , queryServer- > query . , .

, .:)

0
source

Source: https://habr.com/ru/post/1760579/


All Articles