PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
wildwobby wrote:is it bad to access a class's method with -> ?
No.
So you have to use -> if you are accessing an objects methods but you can use -> or :: out of preference if you are acessing a method within the class?
<?php
error_reporting (E_ALL);
class DB{
private $username = "user";
private $password = "pass";
private $db = "testdb";
private $host = "localhost";
private $link = false;
private function __construct(){
$this->link = mysql_connect($this->host, $this->username, $this->password);
if(!$this->link){
if($config['errors'] == false){
die('DATABASE ERROR: ' . mysql_error());
}else{
die('DATABASE ERROR: Connection refused');
}
}
mysql_select_db($db);
}
public function query($query){
if (isset($query) && (!empty(trim($query)))){
return mysql_query($query) or die('Query Failed.');
}else{
die('WEBSITE ERROR: Mysql query appears to be empty!');
return false;
}
}
public function getObject($result){
return mysql_fetch_object($result);
}
public function prepStr($str){
return mysql_real_escape_string($str);
}
public function close(){
mysql_close($this->link);
}
}
class MNews{
private function __construct(){
$this->db = new DB();
}
public function addNews($title, $content, $authorid){
$this->title = $this->db->prepStr($title);
$this->content = $this->db->prepStr($content);
$this->authorid = (int)$authorid;
$this->time = time();
return $this->db->query("INSERT into news VALUES (, $this->date, $this->title, $this->content, $this->authorid)");
}
public function removeNews($id){
$this->id = (int)$id;
return $this->db->query("DELETE * FROM news WHERE id = $this->id");
}
}
public function editNews($id,$newcontent){
$this->id = (int)$id;
$this->newcontent = $this->db->prepStr($newcontent);
return $this->db->query("UPDATE news SET content=$this->newcontent WHERE id = $this->id");
}
public function getNews($id = false){
if($id){
$this->id = (int)$id;
return $this->db->query("SELECT * FROM news WHERE id=$this->id");
}else{
return $this->db->query("SELECT * FROM news LIMIT 0,10 ORDER BY date DESC");
}
}
}
class Members{
private function __construct(){
$this->db = new DB();
}
private function validateEmail($email){
$this->email = (string)$email;
$this->pattern = "^[_a-zA-Z0-9-]+(\.[_a-zA-Z0-9-]+)*@[a-zA-Z0-9-]+(\.[a-zA-Z0-9-]+)*\.(([0-9]{ 1,3})|([a-zA-Z]{2,3})|(aero|coop|info|museum|name))$";
return preg_match($this->pattern, $this->email);
}
private function getDbPassword($username){
$this->username = $this->db->prepStr($username);
$this->query = $this->db->query("SELECT password FROM users WHERE username=$this->username"
if (mysql_numrows($this->query) != 1){
return false;
}else{
while($this->password = $this->db->getObject($this->query)){
$this->dbpass = $this->password->pass;
$this->salt = $this->password->salt;
}
return array("pass" => $this->dbpass, "salt" => $this->salt);
}
}
private function phash($pass){
$this->pepper = "ayrn339kd0dj9fgy3ndikdd3isznu3sf4gh4";
return $pass.$this->pepper;
}
public function login($username, $pass){
$this->args = $this->getDbPassword($username)
$this->userpass = $this->db->prepStr($this->phash($this->args['salt'].$pass));
if($this->args['pass'] === $this->userpass){
return true;
}else{
return false;
}
}
public function register($username, $password, $email, $content){
}
}
class FrontEnd{
//dummy class
}
class Components extends FrontEnd{
public function getHeader(){
include "../interface/header.php";
}
public function getFooter(){
include "../interface/footer.php";
}
public function getNav(){
include "../interface/navigation.php";
}
public function getLogin(){
include "../interface/login.php";
}
public function getModule($name){
include "../".$name.".php";
}
}
class NewsDisp extends FrontEnd{
function __construct(){
}
}
If a class has non-static member variables and functions, it's pretty safe to assume that it wasn't meant to be used with the scope resolution operator (::).
// Formats text prior to display
class CFormatter
{
static private $_pParser;
/**
* Formats content
* @param String : $content text to format
* @param Array : $rules array of token rules that interact with CTokenRules
*/
static public function FormatContent($content, $rules)
{
$pFoo = new CTokenRules($rules);
self::$_pParser = new CParser($pFoo->GetRules());
return self::$_pParser->Parse();
}
}
I still don't understand when you would use the ::. What would be the point of a function if it is static? Isnt the whole point of php to make things dynamic? Obviously I'm mistaken somewhere....