[Opinion] PHP Class...

Hello, my name is Modestas and I am working with php. So I am newbie in this forum and i want to listen to your opinion about php.(How to do something the best and secured way.)…

Here is my code:

[php]class Withdraw
{
private $errors;

public function connect()
{
mysql_connect( SQL_HOST, SQL_USER, SQL_PASS ) or die ( mysql_error() );
mysql_select_db( SQL_DB ) or die ( mysql_error() );
}

public function __construct()
{
$this->errors = array();

$this->id     = 0;

}

public function getWithDraw( $id, $username )
{
$this->connect();

$data = mysql_query( "SELECT * FROM accounts WHERE id = '$id' AND username = '$username'" );
$list = mysql_fetch_array( $data );

return $list[‘withdraw’];
}

public function show_errors()
{
echo “

Errros

”;
foreach( $this->errors as $key=>$value )
echo $value."
";
}
}[/php]

As you can see, i use only “public” function so i wanted to ask you guys about other strings like “private”,“static” and so on.
So question: is my code secure? In this code what should i use in functions for more secure and better code? or maybe i need to rename functions for more secure class?

This code is only example i just coded it like i usualy code, but i think my coding has some wrong coded parts. :slight_smile: so it would be great if you give me some opinion or examples, how should it look. :stuck_out_tongue:

In another way i want to ask when i nedd to use “static”,“private”,“public”. It would be cool if you give a couple examples.

Hello,

http://php.net/manual/en/language.oop5.static.php explains how to use the static keyword in your classes. Basically using static will allow you to use the variables and methods without needing an instance of the class ( $class = new MyClass(); ).

Public keywords in your classes members and methods allow your scripts to reference your class’s members or use your class’s methods ( $var = $class->var; and echo $class->HelloWorld(); ).

Protected keywords will not allow your scripts to access the members or methods outside of the class. However, they can be passed to child classes through inheritance and used in the child classes. If you want to be able to have your scripts use members or methods outside of the class, you need to have your class use them then pass them out.

Private keywords will not allow your scripts to access the members or methods outside of the class as well as they won’t be passed to child classes through inheritance. They will only stay in the class and only can be used by the class.

You will use private and protected keywords for your members and methods if you don’t want them to be accessed by anything outside of your class. This is good to make sure that only the class and nothing else will be able to use or change any important members or use specific methods when you don’t want them to. Unless you are going to be using inheritance, I would stick with the private keyword only.

Reference this about classes to further your understanding: http://php.net/manual/en/keyword.class.php.

Now to answer your question as to if your code is secure, in what ways are you wanting your code to be secure? Using public, private, static, and protected are used for accessing things inside your classes.

Cheers!

Thanks. “OpzMaster” You answered my question. I writed private message, please check it. :slight_smile:

Sponsor our Newsletter | Privacy Policy | Terms of Service