oikram wrote:
What what does this do?
Since we cannot access the class properties directly, because it would give us some problems (that later on, I hope, we, newbies, will understand), we use a function that actually get the proprieties we want from the class. So, if, in this case, we want to use the property conexao, we will not change the conexao access modifier to "public" and call it like this:
$handlerdb->conexao
Good practice dictates that a object should in control of its own state. When properties are able to manipulated by the outside world the object no longer has control of its own state. However, by using getter and setters the outside world can indirectly modify the objects state while the object is directly able to control that input.
A simple example of the purpose of getter and setters can be seen in the below User class. The user class has a property name. Name should only ever be a string. By using a setter we can control that where as if we were able to set the property directly from the outside world we would be unable to control that. In most other languages all we would need to do is actually type the argument to a string, but PHP isn't f OO oriented just has support for it. For example, a string and integer aren't objects.
Code:
class User {
private $_name;
public function setName($pName) {
if(is_string($pName)) {
$this->_name = $pName;
}
}
public function getName() {
return $this->_name;
}
}
Code:
$user = new User();
$user->setName(3); // not going to set name because it isn't a string
$user->setName('tom'); // sets name because argument is a string
echo $user->getName(); // returns 'tom'
oikram wrote:
So my parent class will be PDO, and when I need to access the public methods or properties of the PDO I will use
parent:repare($this->sql); or PDO:repare($this->sql) for example?
Under this circumstance you may call $this->prepare() inside the subclass because the the prepare method doesn't exist inside the subclass.
Code:
Class ligacaoBD extends PDO {
public function send($pSql) {
$this->prepare($pSql);
}
}
However, if you override the prepare method then you must call it upon the parent.
Code:
Class ligacaoBD extends PDO {
public function prepare($pSql) {
parent::prepare($pSql);
}
}
oikram wrote:
Why using a try catch inside the constructor will help me?
That is just for convenience. You could just as easily place it outside the class when creating a instance.
oikram wrote:
I wish to use a prepare/execute PDO method because I've read that it's more efficient if you want, for example, prepare a query, and then execute it several times, like on a "multiple insert record" scenario.
Well yes but in terms of what your talking about it would be more efficient to do it all at once using one query.
Code:
INSERT INTO TABLE_NAME (id,whatver) VALUES (NULL,'blah'),(NULL,'blah again'),(NULL,'blah blah blah');
oikram wrote:
What is a factory and what does it do?
The below isn't a factory, but the singleton pattern. A singleton only allows you to create one instance of the class. You would generally also declare the constructor as private or protected so that a error would occur if you attempt to instantiate a object of the class from outside the class. Generally this done by using a protected/private static property that holds the instance and a public static method that returns or creates the instance based on whether it has or hasn't been already created. A factory pattern does exist, but it has a completely separate purpose from a singleton.
Code:
<?php
class DatabaseFactory
{
protected static $oConnection;
public static function Build()
{
if((self::$oConnection instanceof PDO) === false)
{
self::$oConnection = new PDO('dsn');
}
return self::$oConnection;
}
}
$oDatabase = DatabaseFactory::Build();
$oDatabase->prepare();
?>
Bookmarks