-
Posts
2,000 -
Joined
-
Last visited
-
Days Won
1
Posts posted by Andy-H
-
-
At the same time as building this I am trying to get better acquainted with OOP, I figured the best way to do that would be to "roll my own", I used an interface rather than defining an abstract method because there is no behaviour in the method defined, and my interfaces always define a public method, I know I can define an abstract method without any body, but I thought I was using abstraction for the purpose it was intended and the same with the interface, as for why the interface is not implemented on the abstract class, an oversight on my part
The getDBH method was implemented on the controller, because I load controllers dynamically, so in order to in ject the dbh, I would have to inject it in cases where a database connection might not be necessary, whereas I can just call $dbh = $this->getDBH() where necessary, then inject that into my models, is there a better way?
I will update the abstract classes to implement the interfaces, thanks for taking the time to review this
And thanks in general, you've helped me out a lot recently
-
I'm writing a CRUD API and want to use MVC (for the first time), just wondering if someone could cast wise-eyes over it and let me know if there's anywhere I'm going wrong, code attached, thanks
-
date('Y-m-d', strtotime('2012-05-21 +1 month'));
-
If anyone has a little spare time could they cast their eye over this and let me know if I'm doing things the right way, thanks .
-
I have changed the output method to a getContents method, which returns rather than echoing so only the controller outputs to the client, still unsure as to whether I need to invoke a model though, thanks for any help.
-
Does a controller necessarily have to invoke a Model or can I just invoke the view straight away or is it bad practice?
<?php /** * Index * * Default controller, invokes when not query string is present * * @version 1.0 * @package com */ namespace com\controllers; class Index extends Abstract_Controller implements Interface_Controller { /** * Index::invoke * Invoke method required by Interface_Controller, output error message * @access public */ public function invoke(array $params) { $view = new \com\views\Index; $view->output('No method requested in query string'); return; } }
-
$Controller = 'Controller\\' . ucfirst($path[0]); $Controller = new $Controller;
Importing is performed at compile-time, and so does not affect dynamic class, function or constant names.
Doesn't work with dynamic names. You'll probably just have to include the full namespace in the string rather than try and do a use statement.
That's an oversight surely? Is this expected behaviour? I would have thought if I double quoted the namespace alias it would interpolate like a variable does :/
Is there a better way than this to load controllers anyway??
Also, I am unsure as to how I would inject objects into methods doing things this way, as my controller can't take arguments, is it common for controllers to contain references to super global arrays or have I overlooked something?
Also, how would you recommend injecting my PDO object into dependant classes?
-
It gets autoloaded
/** * AUTOLOAD CLASSES */ include 'ClassNotFoundException.class.php'; function __autoload($class) { $class = explode('\\', $class); $classname = array_pop($class); $file = APPLICATION_PATH . implode(DIRECTORY_SEPARATOR, $class) . DIRECTORY_SEPARATOR . str_replace('_', DIRECTORY_SEPARATOR, $classname) .'.class.php'; if ( !file_exists($file) ) throw new ClassNotFoundException($file); include $file; }
Catching ClassNotFoundException and echoing getMessage outputs :
/var/www/api.domain.com/Controller/User.class.php
-
I've tried that, it loads but just doesn't resolve Controller to \com\controllers
//EDIT, I also tried double quotes.
-
Tried :
<?php define('ENV', 'DEVELOPMENT'); include '../com/config/bootstrap.php'; // Namespace aliases use \com\controllers as Controller; use \com\models as Model; try { /** * SET NAMESPACE ALIASES */ /** * ESTABLISH CONNECTION TO DATABASE */ $dbh = new PDO('mysql:dbname='. DB .';host='. DB_HOST, DB_USER, DB_PASS); $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'UTF8'"); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); if ( isset($_GET['path']) ) { $path = explode('/', $_GET['path']); $Controller = ucfirst($path[0]); $Controller = new Controller\$Controller; } } catch ( PDOException $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => 'Could not connect to database')); //$e->getMessage(); } catch ( ClassNotFoundException $e ) { echo $e->getMessage(); } catch ( Exception $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => $e->getMessage())); } ?>
Getting unexpected T_NAMESPACE_SEPARATOR expected T_STRING on line 22, thinking maybe this behaviour isn't supported, although I'm sure it should be?
-
Was because use was inside of the curly braces
http://www.php.net/manual/fa/language.namespaces.importing.php#98908
<?php define('ENV', 'DEVELOPMENT'); include '../com/config/bootstrap.php'; use com\controllers as Controller; use \com\models as Model; try { /** * SET NAMESPACE ALIASES */ /** * ESTABLISH CONNECTION TO DATABASE */ $dbh = new PDO('mysql:dbname='. DB .';host='. DB_HOST, DB_USER, DB_PASS); $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'UTF8'"); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); if ( isset($_GET['path']) ) { $path = explode('/', $_GET['path']); $Controller = 'Controller\\'. ucfirst($path[0]); $Controller = new $Controller; } } catch ( PDOException $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => 'Could not connect to database')); //$e->getMessage(); } catch ( ClassNotFoundException $e ) { echo $e->getMessage(); } catch ( Exception $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => $e->getMessage())); } ?>
Works, however, it still doesn't alias it and tries to resolve to Contoller/User.class.php ??
-
Checked apache log, unexpected T_USE in index.php on line 8
-
Just checked php.ini and log errors is off
-
I'm not sure, my works server is set up weird, I have display_errors on and error_reporting E_ALL | E_STRICT but I just get a 500 server error. I don't know why it does this?
-
PHP 5.3.2-1
This works fine:
define('ENV', 'DEVELOPMENT'); include '../com/config/bootstrap.php'; try { /** * SET NAMESPACE ALIASES */ //use \com\controllers as Controller; //use \com\models as Model; /** * ESTABLISH CONNECTION TO DATABASE */ $dbh = new PDO('mysql:dbname='. DB .';host='. DB_HOST, DB_USER, DB_PASS); $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'UTF8'"); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); if ( isset($_GET['path']) ) { $path = explode('/', $_GET['path']); $Controller = '\\com\\controllers\\'. ucfirst($path[0]); $Controller = new $Controller; } } catch ( PDOException $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => 'Could not connect to database')); //$e->getMessage(); } catch ( ClassNotFoundException $e ) { echo $e->getMessage(); } catch ( Exception $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => $e->getMessage())); }
but when I try
define('ENV', 'DEVELOPMENT'); include '../com/config/bootstrap.php'; try { /** * SET NAMESPACE ALIASES */ use \com\controllers as Controller; //use \com\models as Model; /** * ESTABLISH CONNECTION TO DATABASE */ $dbh = new PDO('mysql:dbname='. DB .';host='. DB_HOST, DB_USER, DB_PASS); $dbh->setAttribute(PDO::MYSQL_ATTR_INIT_COMMAND, "SET NAMES 'UTF8'"); $dbh->setAttribute(PDO::ATTR_EMULATE_PREPARES, true); if ( isset($_GET['path']) ) { $path = explode('/', $_GET['path']); $Controller = 'Controller\\'. ucfirst($path[0]); $Controller = new $Controller; } } catch ( PDOException $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => 'Could not connect to database')); //$e->getMessage(); } catch ( ClassNotFoundException $e ) { echo $e->getMessage(); } catch ( Exception $e ) { echo json_encode(array('STATUS' => 'FATAL', 'MESSAGE' => $e->getMessage())); }
I get server error, access denied to page, any ideas why?
Thanks
// EDIT, forgot to uncomment use com\controllers, as soon as I uncomment this, I get the error, regardless of whether I use the aliased namespace or not.
-
There is like 1 repetitive field in there for location type, plus it has input1, input2, input3, input4, input5, input6 and inputi, but as I said, were re-designing the database - the current one was designed before I worked there.
-
We are in the process of re-designing the whole database, I didn't design the current database, no there is no index on ID / device. My boss just asked me if I knew why it was happening then I got interested, I don't think it's a query used anywhere in any application as we normally need locations in chronological order.
-
The first image is for the order by id (20 second) query, the second is order by timestamp 0.000x query.
-
-
Well, we only apply it to vehicles at the moment, but if we find a unit with decent enough battery life, it may be applied to animals etc.
Not just stolen vehicles, we're more likely to query a target units location if the vehicle it's attached to is reported stolen, but we query them all on a regular basis to test whether they're still working.
The fleet units are attached to all kinds of vehicles, vans, trucks, JCB's etc. they just allow a business track their employees on a google map for whatever reason.
The data is inserted via
SMS units send an SMS to a gateway which pipes the data via HTTP to a script that inserts the data into a MySQL database.
The GPRS units send packets to a server we have set up that parses the data and inserts it into a MySQL database.
-
-
We have a database of locations, this query executes fine when run on other devices, but this phone number has 65,000 results, and it takes around 20 seconds to execute when ordered by id (which is a primary key so must be indexed, right?)
Showing rows 0 - 9 (10 total, Query took 20.2472 sec) SELECT * FROM `newlocations` WHERE device = '077********' ORDER BY id DESC LIMIT 10
But when ordered by thetimestamp (unix timestamp, also indexed), it takes < 0.001 second
Showing rows 0 - 9 (10 total, Query took 0.0008 sec) SELECT * FROM `newlocations` WHERE device = '077********' ORDER BY thetimestamp DESC LIMIT 10
Anyone got any idea what could be causing this to happen? As I say, with other phone numbers the query executes reasonably quickly
Showing rows 0 - 9 (10 total, Query took 0.3845 sec) SELECT * FROM `newlocations` WHERE device = '077********' ORDER BY id DESC LIMIT 10
-
Just thinking, should my GPS_location class extend LatLng, it does have a longitude and a latitude, so the operations defined in LatLng will still be applicable, however the data is not directly related, as in a GPS location has a latlng, but the other data is not geographic, it will be bearing, speed, inputs etc. so this leads me to think I should simply inject the GPS_location constructor with a LatLng object, also this API will only output the data in JSON format (as far as I know at the moment), so is this necessary?
Also; if I use PDO::fetchAll() to retrieve a list of latitude and longitudes, then I have to loop through the data to create the LatLng objects, when in actual fact I don't need the behaviour of the object, I simply need to output the latitude and longitude as part of a JSON string, what would be the best way to go about this?
Just realised that PDO has functionality similar to mysql_fetch_object, so I can call
PDOStatement::fetchAll(PDO::FETCH_CLASS, 'LatLng');
-
I got into web development via MMORPG's, although I should omit the first M; I used to play a game called NY-Mafia as a kid and wanted to make my own, got some scripts, started copying and pasting bits to create the functionality I wanted, then reading and writing bits, then I started coding full add-on scripts. Now I'm a junior web developer, by no means a great web developer, but I aspire to be
I had my own game for a while, then gave the scripts away, I don't have them any more, it was called NY-Mobster (imaginative, I know lol), maybe you can get hold of them somewhere, but it's a pain in the arse to run one, and you definitely won't make any substantial amount of money, I would make anywhere up to 100 pounds a month, but would spend 15 hour stints programming features and updates, it's really something you've just got to enjoy doing.
I also feel that I neglected to mention that this very forum played an integral part to my progression as a developer, so if it is a learning experience you're after, stick around here, ask questions, answer questions; people will correct you if you get it wrong, but it's all part of the learning process, so don't take it personally, the word 'criticism'; is only seen around here on the right hand side of
echo 'constructive '.
Any idea why namespace aliasing won't work?
in PHP Coding Help
Posted
The idea was to inject PDO into my controllers, but then I'm creating a database connection for every request, what if I don't need one? I get all the concepts of OOP, and know a few design patterns, it's just putting them into use that I always struggle with, maybe I'm concentrating too much on creating the perfect application rather than "an application" :/
I've read a lot about your proem framework, it looks impressive But we're running 5.3, is 5.4 stable now?