Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Jack Ellis

Members
  • Joined

  • Last visited

Reputation Activity

  1. Like
    Jack Ellis reacted to Jock in User Class - Which Design Pattern?   
    Because I wanted to store the user objects within the registry in an array fashion, I made the Users registry object extend the SPL ArrayAccess object, which will make the object 'act' like an array.
     
    So offsetSet($key, $value) is similar to procedurally $array[$key] = $value, and offsetGet($key) is like $array[$key]. Essentially they're just setting and getting an array value. Ultimately this will make our Users object look and act like an array, where the key is the user id and the value is the user object.
     
    The point of all that is that you now have a robust method of storing users and can interface with the class using all sorts of useful ways like you can with an array.
     

    //get the instance of Users $users = Users::getInstance(); //access a indice as like an object echo $users[2]->myMethod(); //count it echo count($users); //traverse it foreach ($users as $user) { echo $user->helloWorld(); }
  2. Like
    Jack Ellis reacted to Jock in User Class - Which Design Pattern?   
    I'd use the registry pattern to hold user object instances.
     

    <?php // example users class class User { private $id; public function __construct($id) { $this->id = $id; } } class Users extends ArrayObject { protected $container = array(); private static $instance; /** * @return Users */ public static function getInstance() { if (!isset(self::$instance)) { $c = __CLASS__; self::$instance = new $c; } return self::$instance; } public static function getUser($name) { $instance = self::getInstance(); if ($instance->offsetExists($name)) { return $instance->offsetGet($name); } else { $user = new User($name); $instance->offsetSet($name, $user); return $user; } } } Users::getUser(1); Users::getUser(2); Users::getUser(3); Users::getUser(1); Users::getUser(1); Users::getUser(1); Users::getUser(1); $users = Users::getInstance(); echo count($users);
  3. Like
    Jack Ellis got a reaction from pippin in no $_GET on page refresh   
    Not 100% sober but chucked this code together for you matey.
     

    <?php session_start(); if($_GET['act'] == "char_con") { if ($_GET['first'] == 'true') { $con = mysql_connect("#", "#", "#"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("quote", $con); $result = mysql_query( ' SELECT * FROM `quote` ORDER BY RAND()' ); $row = mysql_fetch_array($result); $_SESSION['quote'] = $row['q_quote']; header('Location:index.php?act=char_con&beginCount&cb=%3C?php%20echo%20time();?%3E'); mysql_close($con); } else { echo $_SESSION['quote'] } } ?>
     
    and then have your LINK as
    <a href="index.php?act=char_con&first=true&beginCount&cb=%3C?php%20echo%20time();?%3E">
    LINK
    </a>';
     
    Basically what I did was add an extra $_GET parameter ("first") to check if it was from the link.
     
    Remember to keep session_start at top of document. I also got rid of the while because you were only getting one quote out so there was no need for a while?
     
  4. Like
    Jack Ellis reacted to Wickham in Who here uses HTML 5 now?   
    Have a look at this:-
    http://www.findmebyip.com/litmus/
    HTML5 and CSS3 support is getting better, Firefox, Safari, Chrome and Opera are already quite good and IE9 beta is about the same but IE8 and lower are very bad.
     
    I use HTML5/CSS3 features where they won't do any harm if they don't work (there're just display features) like border-radius, text-shadow, box-shadow, multi-columns, @font-face and a few others.
     
    I don't think it's a good idea to use <video> or <audio> and other tags like <header>, <footer> and <canvas> as they won't process in many browsers and will wreck your page.
  5. Like
    Jack Ellis reacted to J90 in Who here uses HTML 5 now?   
    HTML 5 is the future, so I've heard.
  6. Like
    Jack Ellis reacted to FizixRichard in Classes Vs Non OOP way   
    I'll explain OOP in a different way, as I think it puts OOP into context and its benefits and how it works into a meaninful way. It might get a bit long, but it should help getting the OO concepts to make sense and how they are meant to be used.
     
    Edit: Wow that got long...
     
    I learned the basics of OO in the 90's using C++ for simple applications. Later I created a few basic Java Applets which were also OO.
     
    Where I came to fully understand OO and where it all truly "clicked" was with a game engine called Unity 3D. I've used Unity since version 1 a fair few years ago, its a 3D game engine for desktop, browser based and now iPhone and Android based games.
     
     
    In that environment OOP becomes essential as the applications are complex, they are real time and there is a hell of a lot going on; even in a simple game. So I'll explain OO in that environment and then PHP.
     
     
    Consider a simple 3D game set across 10 levels with a playable character that has to navigate around a 3D area that contains monsters. I think monsters in games explain OO really well.
     
     
    Each level will have many monsters, there will be multiple versions of the same monster that have different attacks, different strength, different defence and different AI behaviours.
     
     
    Like say an Orc, some use an Axe and are physically strong, some use a bow and arrow, like to attack from range and will hunt you down.
     
     
    They will have things in common as well; they both use the same mesh, maybe different textures, they will move the same way, they share some common skills, they detect you in the same way.
     
     
    Here we have how OOP works and comes into its own...
     
     
    You have a real time, 3D environment. You have two types of Orc (Axe Orc and Bow Orc). They are both orcs and if you created them as unique objects they'd share the same code (repitition) so you'd create an Orc Object (class) and have two child classes (Axe Orc and Bow Orc) inherited classes.
     
     
    In OOP you can create parent classes and child classes that inherit everything from its parent. Like so:
     
     

    Class Orc { ... Behaviours that are common to all orcs regardless of type, i.e. the mesh, animation details, how they detect the player, base HP and defence ... } Class AxeOrc extends Orc { ... Behaviours that are specific to Axe Orcs such as "Axe Blast" attack, additional strength modifier ... } Class BowOrc extends Orc { ... Behaviours that are specific to Bow Orcs such as "Ranged Attack", additional agility modifier ... }
     
    In this example, using OO allows you to create an Orc class and then child classes for every type of Orc. This allows you to cut the size of the code down, have global behaviours contained in the parent and behaviours that are specific to a certain type of Orc in child classes.
     
    This simplifies the code and reduces code repitition.
     
     
     
    Using functional decomposition, this scenario would be more complex and you'd have code repitition:
     
     

    function AxeOrc { ... Behaviours that are common to all orcs, i.e. the mesh, animation details, how they detect the player, base HP and defence ... ... Behaviours that are specific to Axe Orcs such as "Axe Blast" attack, ... } function BowOrc { ... Behaviours that are common to all orcs, i.e. the mesh, animation details, how they detect the player, base HP and defence ... ... Behaviours that are specific to Bow Orcs such as "Ranged Attack" ... }
     
    In this example the two functions are almost identical with subtle differences. You have a lot of code repition.
     
     
    Now lets say you wanted to add a new skill to ALL types of Orc. In the OO example you'd add the new skill to the Orc class and all child classes would automatically gain that skill.
     
    In the functional decomposition example you'd need to add the skill to ALL of the Orc type functions.
     
     
    Also in OOP the functions for Orc's are contained within the class which nests them together in a tidy little package.
     
     
    So class Orc would have a bunch of functions for Orc that only Orc objects can use.
    The child classes would contain a bunch of functions that only say AxeOrc can use.
     
     
    Now consider this, you have "grunts", some of them have an Axe and have an attack called "Axe Blast", but its totally different from the Orcs.
     
    In OO you'd have:
     

    class Orc { } class AxeOrc extends Orc { function AxeBlast() { } } class Grunt { } class AxeGrunt extends Grunt { function AxeBlast() { } }
     
     
    There you have two functions of the same name in two different classes, in functional decomposition you'd have:
     

    function OrcAxeBlast() { } function GruntAxeBlast() { }
     
    This is messier.
     
     
     
    Now for another concept, instantiation. In OO you can create unique instances of an object (class). So you can create say 10 instances of AxeOrc, they are all unique, independant instances. This is a major concept especially in a real time application where you have have multiple instances of the same object appearing, disapearing and reapearing in different places.
     
    Then different instances can have slightly different behaviours themselves, say different route finding etc.
     
     
    I think the benefits of OO speak for themselves here, although its a simple "application" it is quite complex and performing these operations using functional decomposition would be very, very messy.
     
    It's this kind of scenario that OOP was devised to tackle. Scenarios where you have multiple instances of the same object, behaving in different ways and being independant of one and other in a real time or semi-realtime environment where stuff is changing a lot.
     
     
    Even in a game using Unity you're not ALWAYS using OOP, you have functional decomposition too. Some things lend themselves to OOP and others to Functional Decomposition.
     
    Also consider a full game, how much is going on, the massive amount of variables and data thats being passed around, how many objects are appearing, how many different types of object appear and how you have "variants" of the same object or objects that share common functions.
     
    OO lends itself to this scenario.
     
     
    PHP is different, for one it is by design procedural (hence why I used the term functional decomposition). Even an OO application in PHP is procedural, its not that fluid and is not ever changing, its all pre-processed and ran through procedurally.
     
     
    It's also rarely having to deal with incredibly complex problems, at least not in the same way as a game or big desktop application like Adobe Photoshop.
     
    In things like games or big desktop applications there are lots of scenarios where functional decomposition either doesn't work or doesn't work well. There is almost always a way to do it, but that way is going to be a hell of a lot more complicated and repetative. The code will also not flow as well.
     
     
    Also some myths...
     
    1. OO does not make cleaner code, Functional Decomposition does not make messy or spaghetti code. You can have clean, easy to read, well formed code using both methods. You can also have messy, spaghetti code using both methods. Thats all down to the programmers, how they structure the application and how well designed the application is.
     
     
    2. OO does not make code read better, Functional Decomposition does not make code read poorly. This is entirely down to how the programmers name their classes or functions. Both OO and Functional Decomposition can be virtually unreadable, they can also be very readable and self explanitory.
     
     
    3. OO does not remove code repitition, Functional Decomposition does not create repitition. This too is down to the programmer. Using both OO or Functional Decomposition you can have repitition or reusable classes/functions.
     
     
    4. OO does not make code extendable, Functional Decomposition does not make code difficult to extend. Again its down to design and how the programmers create their classes and functions.
     
     
    However in certain environments such as games and complex desktop applications using OO lends itself to making code cleaner, more readable, less repetative and more extendable provided the application is well designed. Functional Decomposition in certain environments (such as the Orc example) would lead to repitition and poor naming convention, extension would require changes in many placed by default.
     
     
    If we consider a PHP application, even a complex one like a forum, if your application is designed well and you consider readability and code structure then you're not really going to gain all that much from OOP over Functional Decomposition. Not in any real, quantifiable terms anyway.
     
     
    There are no technological benefits with PHP applications, code can be very readable either way (or unreadable), code can be reused and extended easilly either way (or un reusable and unextendable).
     
     
    This is because, quite frankly most PHP applications just are not that complex, not in comparison to say Adobe Photoshop or Grand Theft Auto IV. There is a lot less going on, things are way more linear, things are not ever changing, you don't have that many variations of the same thing with broad behaviour differences, functions just aren't ever that big (a complete monster controller in Unity with full AI & pathfinding would make wordpress look like "hello world").
     
     
    Take a blog, you dont NEED OOP to make it work well, be structured well, flow well, be meaningful, be extendable etc. etc.
     
     
    i.e.
    Users, quite simple, even with user groups.
    Categories, again quite simple, little variation.
    Blog Posts, again very simple things with little variation.
    Comments, again very simple with little variation.
     
    You don't have many types of blog post or category or comment or user with many different behaviours, things don't suddenly appear as the application is preprocessed. In this environment OOP does not really give you anything.
     
     
    That said, I use OOP, but not in a pre sense. Our internal framework for example uses OO for the folloing things:
     
    1. The bootstrap that ties everything together
    2. Database connectivity (as that supports multiple types of database)
    3. The template engine
    4. Users and usergroups & permissions
     
     
    These don't even need to be in OO though, the functional version is just as readable, just as extendable and performs much the same way. In fact the code looks and is structured very similarly (mainly due to how the functional decomp version is structured deliberately to be reusable and extendable).
     
    We use then create new classes for complex datasets, but not for functionality all the time. Its just needed nor beneficial.
     
    Having an OO platform doesn't give us anything over functional decomposition.
     
    So bottom line, your choice. Its PHP, a procedural, preprocessed scripting language used for what are in effect simple web based applications. Go with what you will do a better job with.
     
    Sometimes however the application will have specific features that lend themselves to OO, that does not mean the whole thing must always be OO. It also does not make you a bad or amateur programmer if you don't use OO.
     
    It would if you were making a game in Unity or a complex desktop app, but not in this environment as its not required and you don't gain anything from it.
  7. Like
    Jack Ellis reacted to Jock in Classes Vs Non OOP way   
    ok
  8. Like
    Jack Ellis reacted to sunwukung in Singleton Pattern - A Few Questions   
    Singletons are a bit of a marmite pattern...people who advocate Unit testing are generally against them, because they promote tight coupling to global resources which makes it hard to test classes in isolation. The need for static methods points to the real problem - resolving dependencies.
     
    Singletons DO have a constructor phase where variables can get initialised. Zend_Registry (and many other application level components) use Singleton - and they get a reference via ::getInstance. You could tie the loading of a config file to that method to get the parameters you need. You could just hard code the connection parameters into the class itself. Personally, I think the solution lies elsewhere - using a Singleton causes more problems than it solves.
     
     
    If you look at this problem another way, the solution may not be to make the class static, rather to have a mechanism in place to load an instance when you need it...
     
    My experience is that Singletons are best used for a specific problem - providing resources globally (and is not always the best method to achieve this*). Rather than creating a Singleton to provide database connections - use a Singleton to pass an instance round your system
     

    class Registry{ private static $instance; private function __clone(){} private function __construct(){ } public static function getInstance() { if( !isset( self::$instance ) ) { self::$instance = new self(); } return self::$instance; } public static function get($key){ $instance = self::getInstance(); if(isset($instance->$key)){ return($instance->$key); } } public function set($key,$value){ $instance = self::getInstance(); $instance->$key = value; } }
     
    now you can set and get a database connection from the registry instead i.e.
     
     

    Registry::set('dbConn',new DB_Connection($config_file)) Registry::get('dbConn');
     
    Memory issues are another concern you expressed. To be honest - loading a database connection class is pretty negligible.
     
    http://phparch.com/2010/03/03/static-methods-vs-singletons-choose-neither/
     
    You can, however, deal with this using a pattern called Lazy Load.
    http://www.devshed.com/c/a/PHP/Lazy-and-Eager-Loading-in-PHP-5/
     
    *I recommend taking a look at dependency injection, but this is a whole other kettle of fish...
    http://fabien.potencier.org/article/11/what-is-dependency-injection
     
    I also recommend taking a look at this great site:
    http://sourcemaking.com/design_patterns
  9. Like
    Jack Ellis reacted to ImADesigner in Hi From Telford   
    I am planning on finishing my job in January and starting a part time job in a shop such as pc world to keep money rolling in, at the same time I will have started the web design business.
     
    I have a fairly good knowledge of web design / coding however I am currently trying to get my head around Magento ecommerce package because that is what I am hoping to be my main seller.
  10. Like
    Jack Ellis reacted to Meow in My Image Upload Site   
    Well, it does what it says it will very nicely!
     
    Is this a permanent upload, or do you purge the server to remove older images?

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.