Using Abstract classes with PHP - php

I'm new to PHP, and was trying to create an Abstract class with a mix of abstract and non-abstract methods, and then extend the class to implement the abstract methods. The following is portions of my two class files:
<?php
require_once 'Zend/Db/Table/Abstract.php';
abstract class ATableModel extends Zend_Db_Table_Abstract {
abstract static function mapValues($post);
abstract static function getTableName();
public static function newEntry($post) {
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$data = mapValues($post, true);
$db->insert(getTableName(), $data);
$id = $db->lastInsertId();
return $id;
}
public static function getEntry($id){
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$db->setFetchMode(Zend_Db::FETCH_OBJ);
return $db->fetchRow("
SELECT *
FROM ".getTableName()."
WHERE ID = '".(int)$id."'
"
);
}
public static function editEntry($id,$post) {
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$data = mapValues($post);
$db->update(getTableName(), $data, " ID = '".(int)$id."' ");
}
public static function deleteEntry($id) {
$db = Zend_Db_Table_Abstract::getDefaultAdapter();
$db->delete(getTableName()," ID = '".(int)$id."' ");
}
}
?>
The child class looks as follows:
<?php
require_once 'Zend/Db/Table/Abstract.php';
class Testing extends ATableModel {
public static function getTableName()
{
return 'TESTING';
}
public static function mapValues($post)
{
$data = array (
'test_description' => htmlentities($post['testDescription'])
);
return $data;
}
}
?>
Both files are located in the same directory relative to one another. However, when I try to run my application, I get the following error:
Fatal error: Class 'ATableModel' not found in /var/www/testApp/application/models/testing.php on line 20
My guess is that there's something wrong with either the order that I'm loading the files, or with where these files are located, relative to one another. However, I'm not sure how to proceed from here. Suggestions?

You're not including the file with your ATableModel definition.
<?php
// in your test
require_once 'Zend/Db/Table/Abstract.php'; // <- should be the file with ATableModel

In your child class you must include() or require() (or require_once()) the class file you are extending. I'm not familiar with Zend however, and if that framework is supposed to include all files in the same directory, don't know.
Try to add some code to make sure the file containing ATableModel is being included.

Related

Can't extend abstract PHP class

I want a base class to be extended, but I have some errors coming out:
Fatal error: Class 'Api\Services\Base' not found in
/var/www/html/Api/Services/Example.php on line 7
I searched for typos, tried to use the fully qualified name, made the abstract class empty or just defined it as a simple class; none of these helped.
Using "require" instead of "use" worked, but still...
Any idea (the two files are in the same directory: /var/www/html/Api/Services)?
Thanks in advance...
<?php
// Base.php
namespace Api\Services;
use Api\Classes\ErrorHandler;
use Api\Classes\ErrorMessage;
abstract class Base
{
public $data = null;
public function getData()
{
return $this->data;
}
public function setData($data = null)
{
$this->data = $data;
}
}
?>
<?php
// Example.php
namespace Api\Services;
use Api\Services\Base;
class Example extends Base
{
public $request = array();
public function __construct($request = array())
{
$this->request = $request;
}
}
?>
use Base
instead of
use Api\Services\Base;
because you are already inside the namespace Api\Services
Actually, you don't even have to write the use statement, you are inside the namespace, you can just call the classes inside the same namespace without including them (use)

PHP use a class variable over several files?

I have 2 files.
One is index.php, which checks for a user login
In this file i create a dbManager class which handles a database.
If dbManager can validate login data, i forward the user to lobby.php (via header).
In lobby.php, i create a Manager class which manages the lobby.
I would like to be able to use the Manager class to forward a query to the DB Manager like this:
index.php
$dbManager = new dbManager();
$userid = $dbManager->validateLogin(($_POST["name"], $_POST["pass"];
if ($userid){
$_SESSION["userid"] = $userid;
header("Location: lobby.php");
}
lobby.php
session_start();
if (isset($_SESSION["userid"])){
$manager = new Manager($_SESSION["userid"]);
$manager->getGames();
}
Class dbManager {
things
}
Class Manager {
public userid;
function __construct($id){
$this->userid = $id;
}
function getGames(){
$ret = $dbManager->queryDB($this->userid);
}
}
I am getting the following notices:
Notice: Undefined variable: dbManager in D:\SecureWAMP_Portable\htdocs\projectX\gameManager.php on line 11
and
Fatal error: Call to a member function getGamesForPlayer() on a non-object in D:\SecureWAMP_Portable\htdocs\projectX\gameManager.php on line 11
What am i doing wrong ?
You should define each class in a new file and include those files (good practice is using autoloading). If you only require one object of a class, you should take a look at the Singleton pattern, as you will always get the same class instance.
For that you define a static protected variable which will hold our class instance and use a static public method to return the class instance and if necessary, create a new class instance. We will define the constructor of each class as private, so the static public method has to be used.
If you have arguments you pass to the constructor, define them for the static public method too and pass the arguments to the constructor in the static public method.
The file DBManager.php will define the class DBManager and will use singleton pattern, as this class will handle connections to the database.
Class DBManager {
static protected $instance = NULL
private __construct() {
//Write the code you want to execute when a new class instance is requested
self::$instance = &$this; //Put a reference to this instance in our static variable
}
//Our static public method to retrieve a class instance
static public app() {
if(self::$instance === NULL OR !is_a(self::$instance, 'DBManager')) { //With is_a we are making sure our self::$instance variable holds a class instance of DBManager
$instance = new DBManager();
}
return self::$instance;
}
/* All your other methods... */
}
Our Manager.php will hold the class Manager and to retrieve a class instance of DBManager we will call our static public method app(). It is valid to also make the class Manager singleton, but only if always only one class instance should exist.
Class Manager {
public userid;
function __construct($id){
$this->userid = $id;
}
function getGames() {
$ret = DBManager::app()->queryDB($this->userid);
}
}
Now our basic index.php just instead of using new DBManager() we will use the static method to return a class instance.
require_once 'DBManager.php';
$dbManager = DBManager::app();
$userid = $dbManager->validateLogin($_POST['name'], $_POST['pass']);
if($userid) {
$_SESSION['userid'] = $userid;
header("Location: lobby.php");
}
In our lobby.php we will use the new Manager class for the first time.
session_start();
require_once 'DBManager.php';
require_once 'Manager.php';
$dbManager = DBManager::app();
if(isset($_SESSION['userid'])) {
$manager = new Manager($_SESSION['userid']);
$manager->getGames();
}
you should not use global vars because it is bad style and will make your life really hard when the application gets bigger.
In your case you want to use the dbManager in different classes.
Since db transaction are likely to be used often, look at the singleton pattern, so there can be only one instance of this class.
But be aware that you should keep the number of singletons as low as possible.
Look into this page to see how the singleton pattern can be implemented in PHP:
http://www.phptherightway.com/pages/Design-Patterns.html
You seem to have a misconception about how php code is distributed and used over multiple files and how these files are used.
For example if you use include() or require() instead of header(), you can refer to the data you have defined so far.
A more advanced approach is to use __autoload() or [spl_autoload_register()](http://php.net/manual/de/function.spl-autoload-register.php) to load classes into your script and have an index.php that starts your script.
(Also read the link #blacksheep_2011 provided)
A good practice is to declare each class in separate file. After that, you need to include the needed class into a file in which you are planning to use that class functionality.
Create file DBManager.php:
Class DBManager {
things
}
Create file Manager.php which will contain Manager class declaration:
include('DBManager.php');
Class Manager {
public userid;
function __construct($id){
$this->userid = $id;
}
function getGames(){
$dbManager = new DBManager();
$ret = $dbManager->queryDB($this->userid);
}
}
Include your classes where they are needed:
index.php
require_once 'DBManager.php';
$dbManager = new DBManager();
$userid = $dbManager->validateLogin(($_POST["name"], $_POST["pass"];
if ($userid){
$_SESSION["userid"] = $userid;
header("Location: lobby.php");
}
lobby.php
session_start();
require_once 'Manager.php';
if (isset($_SESSION["userid"])){
$manager = new Manager($_SESSION["userid"]);
$manager->getGames();
}

PHP: How to properly create an instance of a class within a class? (AKA use a class within a class)

I am reading that the best way to use a class properties (database class) within another class (html class) is to create an instance of that class within the class (html). As to why, I am not sure...but anyway.
How is this done?
I have two scenarios, to see which one(s) are correct and which ones are wrong...
Scenario A
require( database.php );
class html(){
private static $db = null;
private static $page = null;
public function __construct($id){
self::bootstrap($id);
}
public static function bootstrap($id){
self::$db = new database();
self::$page = $db->page($id);
return self::$page;
}
}
//$page = new html('hello-world');
//print $page;
print html::bootstrap('hello-world');
Scenario B
//Class autoloader
spl_autoload_register(function ($class) {
include $_SERVER['DOCUMENT_ROOT'] . '/class/' . $class . '.php';
});
//Scenario B code
class html(){
private static $page = null;
public static function bootstrap($id){
self::$page = database::page($id);
return self::$page;
}
}
print html::bootstrap('hello-world');
Perhaps you have a different scenario that's appropriate, if these are the wrong approach
I would say no scenario is wrong, but scenario B is more appropriate. Since page was designed as static method in database class, that informs the intentional usage of the method.

PHP Object Extension $this->some_object->method

I am trying to make a script in which different classes (e.g. Database, Utilities, Config) are all used to form one central Main class. I have tried extending a chain of them:
Main -> Utilities -> Database -> Configuration
But how can I set the different parts so that they can be called like this:
<?php
$this->db->select("WAFFLES");
echo($this->config->app_path);
?>
You could create a global class that does you basic initializing
class Base {
$var1, var2;
public function __construct() {
$this->var1 = new DB();
$this->var2 = new Config();
....
}
}
Then your classes can extend the base class and have access to the data
class Foo extends Base {
public function bar() {
$this->var1->someOpertaion();
}
}
You need to declare each new object as variable in your Main Class like:
class Main{
private $db = NULL;
private $config = NULL;
$this->db = new Database;
$this->config = new Config;
}
etc.
While i'm not a professional coder i'll considering a better approach than this. This kind of object-handling can cause a bloated main class and in the worst case you may face some performance issues.
1) use __autoload or spl_autoload_register to load classes
2) use magic methods, to call function when getting unknown property. Following examples demonstrates how to use __get and dynamicaly initialize object only when you use them.
//use __autoload to load db and config class when they are called.
class db{
function lol(){
echo 'Hello from db->lol() <br />';
}
}
class config{
function lol(){
echo 'Hello from config->lol() <br />';
}
}
//Manager class to use with classes where you want to access other object trough $this
class Manager{
private $_instances=array();
function __get($name){
//if instance does not exists, create one
if (!isset($this->_instances[$name])){
$this->_instances[$name]=new $name();
}
//return instance
return $this->_instances[$name];
}
}
class Some extends Manager{
function f1(){
$this->db->lol();
$this->config->lol();
}
}
$some=new Some();
$some->f1(); //echoes 'Hello from db->lol()' and 'Hello from config->lol()'
But for accessing global class instances I prefer using following method:
Use singleton pattern to access global class trough GloballClass::i() and if global class is not defined use autoload to load that class.
class db extends mysqli{
private static $_i;
//Access to singleton instance
public static function i() {
return (self::$_i instanceof self)?self::$_i:self::$_i = new self();
}
//class functions
function q($q){
echo 'Hello from db->q()';
}
}
class config{
private static $_i;
//Access to singleton instance
public static function i() {
return (self::$_i instanceof self)?self::$_i:self::$_i = new self();
}
//class functions
function somefunction(){
echo 'Hello from config->somefunction()';
}
}
db::i()->q('SELECT * FROM users');
config::i()->somefunction();
Following is solution inspired by Gordons comment:
It uses GlobalClassFactory class to define only one instance of global classes.
class db{
function lol(){
echo 'Hello from db->lol() <br />';
}
}
class config{
function lol(){
echo 'Hello from config->lol() <br />';
}
}
class GlobalClassFactory{
private static $_classes=array();
public static function getInstance($name){
if (!isset(self::$_classes[$name])){
self::$_classes[$name]=new $name();
}
return self::$_classes[$name];
}
}
class Base{
function __get($name){
return GlobalClassFactory::getInstance($name);
}
}
class Some extends Base{
function f1(){
$this->db->lol();
$this->config->lol();
}
}
$some=new Some();
$some->f1();
Here is the sample prototype:
include 'db.php'; // include db class
include 'config.php'; // include config class
class main{
public $db = NULL;
public $config = NULL;
function __construct() {
$this->db = new db;
$this->config = new config;
}
}
Creating a composite object with instances of everything that might be needed during code execution up front is a complete waste of resources. You want to create instances only when needed. One way to achieve this would be to add a magic __get method to the class:
public function __get($name) {
// if self::$instances (or main) contains instance of $name, return instance
// else if class_exists $name, create, store and return instance
// else throw exception
}
But even then, chances are you are creating a God Object and magic methods are somewhat slower than regular accessors. If you need to create instances this way, have a look at the Symfony Dependency Injection Container or implement a Registry.

How to get the path of a derived class from an inherited method?

How to get the path of the current class, from an inherited method?
I have the following:
<?php // file: /parentDir/class.php
class Parent {
protected function getDir() {
return dirname(__FILE__);
}
}
?>
and
<?php // file: /childDir/class.php
class Child extends Parent {
public function __construct() {
echo $this->getDir();
}
}
$tmp = new Child(); // output: '/parentDir'
?>
The __FILE__ constant always points to the source-file of the file it is in, regardless of inheritance.
I would like to get the name of the path for the derived class.
Is there any elegant way of doing this?
I could do something along the lines of $this->getDir(__FILE__); but that would mean that I have to repeat myself quite often. I'm looking for a method that puts all the logic in the parent class, if possible.
Update:
Accepted solution (by Palantir):
<?php // file: /parentDir/class.php
class Parent {
protected function getDir() {
$reflector = new ReflectionClass(get_class($this));
return dirname($reflector->getFileName());
}
}
?>
Using ReflectionClass::getFileName with this will get you the dirname the class Child is defined on.
$reflector = new ReflectionClass("Child");
$fn = $reflector->getFileName();
return dirname($fn);
You can get the class name of an object with get_class() :)
Yes. Building on Palantir's answer:
class Parent {
protected function getDir() {
$rc = new ReflectionClass(get_class($this));
return dirname($rc->getFileName());
}
}
Don't forget, since 5.5 you can use class keyword for the class name resolution, which would be a lot faster than calling get_class($this). The accepted solution would look like this:
protected function getDir() {
return dirname((new ReflectionClass(static::class))->getFileName());
}
If you are using Composer for autoloading you can retrieve the directory without reflection.
$autoloader = require 'project_root/vendor/autoload.php';
// Use get_called_class() for PHP 5.3 and 5.4
$file = $autoloader->findFile(static::class);
$directory = dirname($file);
<?php // file: /parentDir/class.php
class Parent {
const FILE = __FILE__;
protected function getDir() {
return dirname($this::FILE);
}
}
?>
<?php // file: /childDir/class.php
class Child extends Parent {
const FILE = __FILE__;
public function __construct() {
echo $this->getDir();
}
}
$tmp = new Child(); // output: '/childDir'
?>
Please not that if you need to get the dir, directly use __DIR__.
You can also pass the directory as constructor arg. Not super elegant, but at least you don't have to work with reflection or composer.
Parent:
<?php // file: /parentDir/class.php
class Parent {
private $directory;
public function __construct($directory) {
$this->directory = $directory;
}
protected function getDir() {
return $this->directory;
}
}
?>
Child:
<?php // file: /childDir/class.php
class Child extends Parent {
public function __construct() {
parent::__construct(realpath(__DIR__));
echo $this->getDir();
}
}
?>

Categories