Page 1 of 1

Object questions

Posted: Tue Jul 03, 2007 10:35 pm
by wildwobby
Hello I have a couple questions about php objects and general questions.

First, what is the difference between calling class methods with:

$myclass->getSomthing();

and

$myclass::getSomthing();

Second is it "legal" to have an object initiated within another class's variable and then call methods using
$this->class2->getSomthing():?

If i returned this:

Code: Select all

return array("pass" => $this->dbpass, "salt" => $this->salt);
would it return the array or true?

Thank you!

Re: Object questions

Posted: Tue Jul 03, 2007 10:53 pm
by superdezign
-> accesses an object's methods.
:: accesses a class' methods.

And yes, objects can exist in objects.

Posted: Tue Jul 03, 2007 11:01 pm
by wildwobby
is it bad to access a class's method with -> ?

Posted: Tue Jul 03, 2007 11:14 pm
by superdezign
wildwobby wrote:is it bad to access a class's method with -> ?
No.

Posted: Tue Jul 03, 2007 11:30 pm
by wildwobby
superdezign wrote:
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?

Posted: Tue Jul 03, 2007 11:39 pm
by superdezign
What? No.

-> only works on an object that's been initialized. i.e.

Code: Select all

$pObj->Foo('Bar');
:: only works on class definitions

Code: Select all

CObject::Foo('Bar');
You use :: for static functions that require no member attributes. Most classes, however, are meant to be used to create an object.

Posted: Wed Jul 04, 2007 7:31 am
by wildwobby
So then this, which im currently writing wont work because im using $this->dosomthing(); and not like DB::query("SELECT * FROM example");

?

Code: Select all

<?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(){
	}
}

Posted: Wed Jul 04, 2007 11:01 am
by superdezign
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 (::).

Posted: Wed Jul 04, 2007 11:25 am
by wildwobby
so my code is correct in terms of (not) using that operator? Can I see an example of when you would use the :: then?

Posted: Wed Jul 04, 2007 12:01 pm
by superdezign

Code: Select all

// 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();
    }
}

Posted: Wed Jul 04, 2007 12:07 pm
by wildwobby
sorry,

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....


Thanks for helping me!

Posted: Wed Jul 04, 2007 12:09 pm
by volka
static means that there is no object of the class involved.