I actually took this code from http://php.net. Multiple inheritance has always been a bit dodgy for me in PHP. This is an excellent example of how to use traits to achieve it.
<?php
trait Hello {
public function sayHello() {
echo 'Hello ';
}
}
trait World {
public function sayWorld() {
echo ' World';
}
}
class MyHelloWorld {
use Hello, World;
public function sayExclamationMark() {
echo '!';
}
}
$o = new MyHelloWorld();
$o->sayHello();
$o->sayWorld();
$o->sayExclamationMark();>
?>
Monday, 5 March 2012
Multiple inheritance through PHP using traits
Labels:
inheritance,
multiple,
oop,
PHP
Friday, 2 March 2012
Removing cache manifest in Chrome
In an earlier post, I explained how to use cache-manifest for localstorage. Once you've achieved this, you'll find that in order to continue editing the site that the cache manifest will need to be removed. Otherwise, you won't see your changes. To do this in Chrome, you need to put this command in the address bar:
chrome://appcache-internals/
This will list all the sites in your application cache with a 'remove' link.
A full list of such commands, enter chrome://chrome-urls/
chrome://appcache-internals/
This will list all the sites in your application cache with a 'remove' link.
A full list of such commands, enter chrome://chrome-urls/
Tuesday, 13 December 2011
Invalid field count in csv input on line 1
I came across this annoying phpmyadmin 'feature' when trying to import some data to a newly created MySQL table. I had a CSV file full of data. The columns matched the number of fields but I kept getting the error 'Invalid field count in csv input on line 1'.
The problem was, that the 'Fields terminated by' field in the import screen was set to ';' instead of a ','. By resetting this field, everything worked.
The problem was, that the 'Fields terminated by' field in the import screen was set to ';' instead of a ','. By resetting this field, everything worked.
Monday, 5 December 2011
Reset the Index of a MySQL table
Sometimes, I will be playing about with a MySQL database, often at the start of an application. I'll fire a load of dummy data in and test. When I've finally done with this phase I'm ready to start from the beginning. I might have an ID field which I'd like to auto increment but this time starting from 0. To reset this field, first empty all the records in the table, then you can apply a line like this:
ALTER TABLE `mytable` AUTO_INCREMENT=0
ALTER TABLE `mytable` AUTO_INCREMENT=0
Monday, 7 November 2011
Getting single row results MySQL using PHP
I'm always using this code. Sometimes I know there should be/is only one record returned from my MySQL query. Especially if I have specified 1 result as in the query below. As long as I know the resulting column names, this is a very useful technique.
<?php
$query = mysql_query("SELECT * FROM `users` WHERE `username` = '{$username}' LIMIT 1;");
$row = mysql_fetch_assoc($query);
echo 'Hello '.$row['firstname'];
?>
<?php
$query = mysql_query("SELECT * FROM `users` WHERE `username` = '{$username}' LIMIT 1;");
$row = mysql_fetch_assoc($query);
echo 'Hello '.$row['firstname'];
?>
Tuesday, 1 November 2011
How to use PHP __autoload over multiple directories
Funny this. One of those things where you do a bit of surfing to find the easy answer. The resulting pages of your search contain the name of the solution you are looking for in the title, but the containing code solves a different problem.
So, here is the problem.
I am creating a PHP application which has multiple directories containing classes will need throughout it's build.
I want to use the PHP __autoload function.
I will need 2 PHP files to support this activity, but below this I will show how they are called.
First file, config.php. This will contain a class containing all the support data needed throughout my application.
<?php
class supportdata
{
public $supportArray = array('lib', 'helper');
}
?>
There. Not too difficult was it.
Next, my autoload.php.
<?php
require_once 'config.php';
function __autoload($class_name)
{
$sd = new supportdata;
$classString = '';
foreach($sd->supportArray as $value)
{
$classString = $DOCUMENT_ROOT.$value.'/'.$class_name.'.php';
if(file_exists($classString))
{
require_once $classString;
}
}
}
?>
As you can see it calls config.php and makes use of the supportArray to iterate through the directories, checking to see if the class exists. If it does we do a require_once on it.
Now, here is how my autoloading is called.
<?php
include_once 'autoload.php';
class index
{
function __construct()
{
}
}
new index;
?>
In my index class I can now call any of the classes contained in the directories named in config.php. In fact, I can call any of the those classes, anywhere in my application.
So, here is the problem.
I am creating a PHP application which has multiple directories containing classes will need throughout it's build.
I want to use the PHP __autoload function.
I will need 2 PHP files to support this activity, but below this I will show how they are called.
First file, config.php. This will contain a class containing all the support data needed throughout my application.
<?php
class supportdata
{
public $supportArray = array('lib', 'helper');
}
?>
There. Not too difficult was it.
Next, my autoload.php.
<?php
require_once 'config.php';
function __autoload($class_name)
{
$sd = new supportdata;
$classString = '';
foreach($sd->supportArray as $value)
{
$classString = $DOCUMENT_ROOT.$value.'/'.$class_name.'.php';
if(file_exists($classString))
{
require_once $classString;
}
}
}
?>
As you can see it calls config.php and makes use of the supportArray to iterate through the directories, checking to see if the class exists. If it does we do a require_once on it.
Now, here is how my autoloading is called.
<?php
include_once 'autoload.php';
class index
{
function __construct()
{
}
}
new index;
?>
In my index class I can now call any of the classes contained in the directories named in config.php. In fact, I can call any of the those classes, anywhere in my application.
Labels:
__autoload,
autoload,
classes,
config,
directories,
multiple
Thursday, 20 October 2011
How to use cache-manifest for localstorage
So, you want your website to continue working on someone's laptop, when they're on a train and going through a tunnel.
Here is a technique for doing just that.
First create a .htaccess file for your site and add to it the following line:
AddType text/cache-manifest .manifest
Next, create a file called cache.manifest. In this file, add the lines:
CACHE MANIFEST
index.html
Continue to add entries for any file you wish to be cached such as additional pages, images, scripts, stylesheets etc.
Now let's create the index.html file which will be cached:
<!DOCTYPE html>
<html manifest=”cache.manifest”>
<head>
<title>Cached Page</title>
</head>
<body>
My cached page.
</body>
</html>
There. Wasn't too difficult was it.
Here is a technique for doing just that.
First create a .htaccess file for your site and add to it the following line:
AddType text/cache-manifest .manifest
Next, create a file called cache.manifest. In this file, add the lines:
CACHE MANIFEST
index.html
Continue to add entries for any file you wish to be cached such as additional pages, images, scripts, stylesheets etc.
Now let's create the index.html file which will be cached:
<!DOCTYPE html>
<html manifest=”cache.manifest”>
<head>
<title>Cached Page</title>
</head>
<body>
My cached page.
</body>
</html>
There. Wasn't too difficult was it.
Subscribe to:
Posts (Atom)