Jump to content
Larry Ullman's Book Forums
Sign in to follow this  
Antonio Conte

Questions About Constructors And Objects

Recommended Posts

Hey,

 

I'm working on a class to display information about football players. I have a little trouble understanding how I can make all players an individual objects though. Now the class is just printing out every player from the database as a string. Would it be possible to create every player as an object, and use the getters and setters I've created? Or is this not the correct use of objects and methods?

 

I've seen in Java that you can pass an Object as a parameter for a method. Is this also possible in PHP? I don't really understand the point in instance variables as they are never user. Likewise with object variables like $this->firstName as they are never used.

 

Am I doing this very wrong? Do I only need objects for classes and methods? Please enlighten me a little, because I really don't understand it. :)

 

Here is my class:

 

<?php

class Players extends Connect {

private $id;
private $firstName;
private $lastName;
private $nickName;
private $birthDay;
private $birthPlace;
private $nationality;
private $position;

public function __construct($id, $firstName, $lastName, $nickName, $birthDay, $birthPlace, $nationality, $position) {
	$this->id = $id;
	$this->firstName = $firstName;
	$this->lastName = $lastName;
	$this->nickName = $nickName;
	$this->birthDay = $birthDay;
	$this->birthPlace = $birthPlace;
	$this->nationality = $nationality;
	$this->position = $position;
}

public function getAllPlayers() {
	$mysqli = Connect::MYSQLI();
	$query = "SELECT *, (YEAR(now())-YEAR(a.dato)) as date FROM abc_players as a";
	$stmt = $mysqli->prepare($query);
	$stmt->execute();
	$stmt->bind_result($id, $firstname, $lastname, $birthplace, $country, $date, $position, $year);
	while($stmt->fetch()) {
		echo '
		<div class="row">
			<div>'.$id.'</div>
			<div>'.$firstname.' <strong>'.$lastname.'</strong></div>
			<div>'.$birthplace.'</div>
			<div>'.$country.'</div>
			<div>'.$date.' år</div>
			<div>'.$posisjon.'</div>
			<div>'.$year.'</div>
		</div>
		<hr />
			';
         }
         $stmt->close();
}

// return firstname
public function getFirstName() { 
	echo '<div>First Name: ' . $this->firstName . '</div>';
}
// return lastname
public function getLastName() {
	echo '<div>Last Name: ' . $this->lastName . '</div>';
}

/* OTHER GETTERS ...... */

// set firstname
public function setFirstName($newFirstName) {
	$this->firstName = $newFirstName; 

}

// set lastname
public function setLastName($newLastName) {
	$this->lastName = $newLastName;
}

/* OTHER SETTERS ...... */

}


// Initialize object of class 
$players = new Players();

// method to get all players from database
$players->getAllPlayers();

?>

Share this post


Link to post
Share on other sites

If you want to create an instance of the class for each player, you'd have to declare a variable for each player, such as:

 

$Pele = new Players(arguments-here);
$Ronaldo = new Players(different-arguments-here);

 

Anyway, not sure what your intentions are, but you might want to take the getAllPlayers method out of the class, and then use that function to create an array of all the players on the fly. If you do that, your getters and setters should work fine.

Share this post


Link to post
Share on other sites

Anyway, not sure what your intentions are, but you might want to take the getAllPlayers method out of the class, and then use that function to create an array of all the players on the fly. If you do that, your getters and setters should work fine.

 

Me neighter, to be honest...

 

The point here being, these getter and setter methods was used a lot in my Java programming class. I wondered wheter they could actually work with info from a database. Even if I move the method 'getAllPlayers' from the script and add players to an array, the database values good not be changed directly, right?

 

So I really don't see the point of them, that is, in this context. I would have to write add, edit and delete methods for players using mysqli anyway, right? THIS is perhaps a better way to descibe what I tried to ask for. :)

 

Thanks for the answeer btw.

Share this post


Link to post
Share on other sites

To be honest, things like getters and setters are quite often not necessary for small projects that only you're working on. For that matter, OOP is often not necessary in PHP either. Hehe! Don't mean to get you down; I know you're trying to learn this stuff.

 

I don't mean to say that OOP is worthless, but it has it's place.

 

To answer your question, getters and setters are very valuable for large projects and modules, etc. Getters and setters allow you to achieve encapsulation, which is a huge perk of OOP. Basically, if you release a library/module that doesn't use getters/setters, a bunch of people start using that library, and then you decide to upgrade that library at a later time, since the people using the library are directly accessing all your class variables, if you change any of them, the users using your old library cannot upgrade without having to change a lot of their own code. That's bad!

 

Instead, by using getters and setters, the method for retrieving information on the library-user's end is always the same, even if you decide to edit the actual inner-workings of the class at a later time. For example, maybe you decide to add validation steps to a variable in your class. That's easy to do when using getters, setters and encapsulation, and you don't have to worry about messing up other people's code.

 

I hope that makes sense. Years ago, when I first learned about the whole public/private/protected thing and encapsulation thing in C++, I didn't get the point, but that's because I had never worked on any huge projects.

 

I think that's the bottom line though: OOP definitely has it's place, but for my own uses at home, making my own small sites, it quite often isn't necessary. There are, of course, those hardcore programmers out there that insistent that you always have to use OOP, but in general, OOP adds another level of abstraction, complexity, and is actually slower than a traditional procedural approach.

 

Back to answering your questions, you'd probably use getters and setters with database info in the sense that you'd retrieve data from a database, store it in a private variable, and after potentially messing with the data in the variable, allow the variable to be accessed via a getter and setter.

 

In the end, if you want to go OOP, you'll want a way of automatically generating hundreds of players on the fly quickly and easily. Then, you can access any "player", and use the native methods attached to the player objects. Personally though, unless you're planning on making this for other people to use, I would probably just directly grab all the necessary data from a database, and edit/display it as necessary. Basically, I wouldn't go OOP with it.

 

It's up to you though.

  • Upvote 2

Share this post


Link to post
Share on other sites

It's more for the learning part. I do a little Java too, and this just seemed natural from what I've learned.

 

I just want an easy workflow. This project is going to manage:

 

- Football seasons (EU kind)

- leagues

- teams

- matches

- players

- player statistics

 

Because of that, I wanted to see if I could possible use getters and setters for editing/adding/deleting data without writing all the queries. Guess I just gotta keep on writing code. ;)

Share this post


Link to post
Share on other sites

Sure, I understand, and I figured it was more for the sake of learning than anything. Generally, those that come from a C++/Java background what to do everything OOP, which is all fine and dandy, but it's not necessarily required.

 

Also, yeah, what you want to do is completely practical; add/edit player info in the database by simply passing arguments to a setter function. You can also use things like stored procedures though, which will allow you to add an extra level of security and speed to your site/app.

 

Anyway, I suppose this is true regardless of the content, but when you say you want to manage all that EU football stuff, I would think you'd simply want to be able to display and sort data, and then by clicking on a player, edit that players content. If that's the case, you really don't need OOP. However, like you said, you're doing it to learn, and that's totally fine. If that's the case though, I'd take the getAllPlayers method out of the Player class. Remember that the methods should be for acting on individual players (if you go OOP on this).

Share this post


Link to post
Share on other sites

Thank you, HartleySan. I don't absolutely need to go OOP on this. It's more about learning, gaining coding experience, and develop in a higher speed.

 

What I mean by speed is, once I've established a certain way of getting, editing, adding and deleting data, from there, it's more about changing table rows in the prepared statements. This is my first rewrite of a functioning administrative environment for the the things listed in the above post. It's secured by .htaccess in an unknown subdomain on a small site, so the security measures is probably don't even needed. :)

 

In the end, it's about learning.

Share this post


Link to post
Share on other sites

Sorry for the delay, but HartleySan was helping out and I figured I'd see how things went before I chimed in. Some notes on what you've done...

class Players extends Connect {

 

I don't know what the Connect class is, but I suspect it's not appropriate for Players to extend Connect unless Player is a specific type of Connect. Maybe what you're looking for here is an interface, but that may not be right either.

 

	public function getAllPlayers() {
	$mysqli = Connect::MYSQLI();

 

Two things here: first, you need to think about the design. My inclination would be that you should create one class type Player and then create one instance of that type for each Player. Second, you've got the connection to the database buried in a class, but it'll no doubt depend upon external values (such as the connection parameters), which isn't good.

 

Your getters and setters look fine to me.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...