0

Im gonna edit the question to make it clearer, so you can see what i have got now, and get an easier understanding of the problem.

<?php
$mysqli = new mysqli("localhost", "user", "password", "test");
class building
{
private $mysqli;
public $buildingid;
public $userid;
public $buildinglevel;




public function __construct($buildingid, $userid, \mysqli $mysqli)
{
    $this->buildinglevel;
    $this->mysqli = $mysqli;
}

public function getLevel()
{
    return $this->mysqli->query("SELECT ".$this->buildingid." FROM worlds WHERE city_userid=".$this->userid."");
}
}

}
?>

Then I use this to create and use the function:

$cityHall = new building("cityHall",$user['id'],$mysqli);
echo $cityHall->getLevel();

This turns out blank, and nothing happens.

4
  • 3
    Do it in the constructor, that's what it is made for. Commented Oct 10, 2013 at 11:03
  • Ive tried to add it inside the constructor, but i dont wanna have to redefine the values of $mysqli everytime, seems like a waste of time? SO basically, how can i keep the $mysqli outside the Class, and import it, to be able to perform the query inside the constructor? Commented Oct 10, 2013 at 11:04
  • Questions asking for code should include attempted solutions, what those solutions did, and what you expected to happen. (aka "Someone must have a minimal understanding of a problem to ask a question about it on SO") Commented Oct 10, 2013 at 11:07
  • @Narf While you can do computations in constructor, this is a terrible idea that makes unit-testing almost impossible. Constructors are meant to initialize class state, basically (i.e inject appropriate dependencies) Commented Oct 10, 2013 at 11:12

3 Answers 3

3

You should inject instance of mysqli to __construct() of building class:

$mysqli = new mysqli('user', 'password', 'localhost', 'test');

if ($mysqli->connect_errno) { printf("Connect failed: %s\n", $mysqli->connect_error); }

class building
{
private $mysql;
private $buildingid;
private $userid;

// I need to have a mysqli_query here to get the info for the correct building, 
//to be able to set the "buildinglevel" for each object from the MYSQL DB, seems basic   
//but none of the things ive tried has worked.


public function __construct($buildingid, $userid, $mysqli)
{
    $this->buildinglevel;
    $this->mysqli = $mysqli;
    $this->userid = (int)$userid;
    $this->buildingid= (int)$buildingid;
}

public function getLevel()
{
    $query = $this->mysqli->query("SELECT ".$this->buildingid." FROM worlds WHERE city_userid=".$this->userid);
    $row = $query->fetch_assoc();
    if (!$query) {
        return $this->mysqli->error;
    }
    if ($query->num_rows == 0) {
        return 'no database records found';
    }

    return $row;
}

}

$Bulding = new building("cityHall", $user['id'], $mysqli);
$level = $Bulding->getLevel();
var_dump($level);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for this answear, but if u have time, would u please check out my edited question? Im new here, and im not sure if i was supposed to do that, but, its more clear to see everything there, and i still have a problem in getting a blank result!
1

Objects are units that encapsulate behavior which is exposed to other objects via methods. A wrapper around public properties retrieved from the DB does not object-oriented programming make. In fact, mysqli can do this for you via fetch_object:

$result = $mysqli->query($query);
while ($building = $result->fetch_object()) {
    // access properties via $building->buildingid, etc.
}

Unless the building class actually offers functionality via methods, and implements some abstraction, it's not needed. What you can have instead is a DAO (Data Access Object) that wraps the DB (mysqli) and the data it retrieves is used by your model.

interface Dao {
    public function get($id);
}

class BuildingDao implements Dao {
    // DB can be a wrapper for mysqli
    // but servers as an interface so it
    // can be replaced easily
    public function __construct(DB $db) {
        $this->db = $db;
    }

    public function get($id) {
        return $this->db->prepare(
            "SELECT buildinglevel FROM building WHERE buildingid = ?"
        )->execute($id)->fetch();
    }
}

Comments

0

What your class there seems to be, is what’s known as a model: it represents some form of data, in your case a particular building.

One approach is to pass the MySQLi object as a constructor object, as well as the ID of the building you’re wanting to query for, and assign the result to class properties. This would look as follows:

<?php
class Building
{
    private $db;

    protected $id;
    protected $level;

    public function __construct(mysqli $db, $id = null)
    {
        $this->db = $db;

        if (!is_null($id) && intval($id) > 0) {
            $stmt = $this->db->prepare("SELECT buildingid, buildinglevel FROM building WHERE `id` = ?");
            $stmt->bind_param('i', $id);
            $stmt->execute();
            $stmt->bind_result($this->id, $this->level);
            $stmt->fetch();
        }
    }

    public function getId()
    {
        return (int)$this->id;
    }

    public function getLevel()
    {
        return (int)$this->level;
    }
}

You can then fetch your building’s properties like this:

$building = new Building($mysqli, 1);

printf('Building ID is %d and building level is %d', $building->getId(), $building->getLevel());

Comments

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.