July 25, 2013

Does it make sense to clone copies of dependencies (Dependency Injection)?

Maxiscool’s Question:

Say I have a class that has a few methods that depend on another object in order to carry out their duty. The difference being they all depend on the same class of object, but need different instances of the class. Or more specifically, each method needs a clean instance of the class because the methods will be modifying the state of the dependency.

Here is a simple example I had in mind.

class Dependency {
    public $Property;
}


class Something {
    public function doSomething() {
        // Do stuff
        $dep = new Dependency();
        $dep->Property = 'blah';
    }

    public function doSomethingElse() {
       // Do different stuff
       $dep = new Dependency(); 
       $dep->Property = 'blah blah';
    }
}

Technically I could do this.

class Something {
    public function doSomething(Dependency $dep = null) {
        $dep = $dep && is_null($dep->Property) ? $dep : new Dependency();
        $dep->Property = 'blah';
    }

    public function doSomethingElse(Dependency $dep = null) {
       $dep = $dep && is_null($dep->Property) ? $dep : new Dependency();
       $dep->Property = 'blah blah';
    }
}

My trouble here is I constantly have to check that the dependent object being passed in is in the correct state. The state being newly created. So instead I was thinking of just doing something like this.

class Something {
    protected $DepObj;

    public function __construct(Dependency $dep) {
        $this->DepObj = $dep && is_null($dep->Property) ? $dep : new Dependency();
    }
    public function doSomething() {
        // Do stuff
        $dep = clone $this->DepObj;
        $dep->Property = 'blah';
    }

    public function doSomethingElse() {
       // Do different stuff
       $dep = clone $this->DepObj;
       $dep->Property = 'blah blah';
    }
}

This allows me to get one instance of an object in the correct state, and I can just copy it if I need another one. I’m just curious if this makes sense, or if I’m over looking a fundamental guideline about dependency injection and keeping code testable.

If doSomething() and doSomethingElse() both does similar things in two functions, then its a bad choice. It this case created a feeder function and feed two different object to this function.

But if there are both responsible for two different action requiring a clone of the dependency object, then it is not bad.

March 6, 2013

CSS child bigger than parent allowed?

Question by Programista

Quick question.

I know i can make child div bigger than parent, but is it allowed by w3c? Is it against some rules? Any performance problems?

Answer by albertxing

There are many cases where you would need an overflow in the parent div, with a larger child div: for example, a fixed-position content frame.

And there should be no performance impediment; it should be around the same as rendering the child div as a sibling of the parent.

Answer by Starx

W3C specifications does not limit you what kind of value you use for its attributes, except for few cases like:

  • Integer Id Attribute

So, to answer your question, Yes it is allowed.

April 25, 2012

Captions on an image with CSS

Question by StickFigs

I’m trying to make “meme”-looking captioned images with CSS using an <img> object and two <p> objects styled with CSS. Right now I have a <div> which contains the <img> and then the two <p>‘s. What I want to do is have the picture positioned in the top-left corner of the div and then I set the z-index to -1 and THEN somehow position the two p objects over the image but positioned relative to the top-left corner of the div.

This is so I can set the top: y and left: x values of the p objects such that they will be positioned accordingly relative to the top-left of the div and in turn the image.

But when I try to set position: relative for the p objects there’s a problem where the first p is correctly placed but the 2nd p is positioned relative to the first one so even though it’s at top: 0, left: 0 it’s still lower than it should be.

How can I fix this?

Answer by Kirean

check out this jsfiddle:

http://jsfiddle.net/56J8y/1/

relevant CSS

.meme_container {
 position: relative;   
}
.meme_container .top_meme {
    position:absolute;
    top:0;
    left:0;
}
.meme_container .bottom_meme {
    position:absolute;
    bottom:0;
    left:0;
}

and the html

<div class="meme_container">
<img src="https://www.google.com/images/srpr/logo3w.png"/>
<p class="top_meme">This is a caption 1</p>
<p class="bottom_meme">This is a caption 2</p>
</div>​

Answer by Starx

One way is to use Pseudo element like :after and :before

Example:

img:after {
   content: "Hello, I am the caption";
}
April 11, 2012

Is there any research/data that shows DIV (with CSS) is faster than TABLE?

Question by deathlock

I’ve seen that similar questions have been asked before, such as: For tabular data, what renders faster, CSS or <TABLE>? or Why not use tables for layout in HTML?

However, besides being a rather old questions (2-4 years ago), I am wondering if there is actually a research/a data which proves with number that (with CSS) renders faster than for displaying tabular data.

I’m not trying to be offensive/disregarding the notion that DIV + CSS is faster (or table is more appropriate), I’m only wanting to know for practical purposes.

I also am not intending this to be a discussion/debate. A link to numbers is what I was expecting.

.

Like previous questions, I’m also aware that to display tabular data it is better to use table. But I just want to know which one is actually, statistically, faster.

Pardon my English, not a native speaker. Sorry if there is any offense taken.

Answer by Starx

Update:

The Discussion about Tables Or Divs have been long fought and left unsettled. As mentioned in your question yourself, the table is more logical when used to show tabular data.

Apart from this as we are moving on to HTML5, the elements are more logical then just styling components. Lets divs divide a portion and Let table display data.


Answering the Misleading Title

CSS is a stylesheet and Table is Element. The comparison between these two are obsolete.###

Read this article. This approaches different then articles normally do.

March 4, 2012

Database class design

Question by JamWaffles

I’m creating a web app with various classes for things like the user, Smarty template control, etc.

I already have a database class which is all well and good, but I’m concerned about the performance of it.

Currently, in another class, I’m doing $this->db = new DB() to create a local database instance, however the database class’s __construct() function creates a new connection to the MySQL server every time I make a new DB() instance, which is obviously less than sensible. This means that each instance of all my different classes that uses the database class makes a connection to the server. I don’t have a vast amount of classes, but I only want one per page load.

This is a stripped down sample of what I have at the moment:

// Database class used by multiple other classes
class DB {
    private $dbh;

    function __construct() {
        $this->dbh = // PDO connection here
    }

    public function query($str) {
        // Do a query
    }
}

// Example class User
class User {
    private $db;    // Stores local instance of DB class.

    function __construct() {
        $this->db = new DB();    // Makes a new connection in DB::__construct()
    }

    public function login() {
        $this->db->query('SELECT * FROM users');
    }
}

I’m looking for the “best” or most common practice of doing this. I don’t want to make 10-ish separate connections for each page load.

I want to know what the best way of using and managing a DB class in my application. My four thoughts are these:

  1. Would using a persistent connection to the MySQL server solve this multiple connection issue for me?
  2. Should I use a static factory class and return a DB instance instead of using new DB()?
  3. Is the proper solution to use an entirely static class and just do DB::query() (for example) every time I reference it?
  4. I often use multiple classes in another (so we might have class Folders which requires classes User, DB and Smarty). Is it general practice to extend each class somehow?

Answer by Brent Baisley

If you make the variable holding the connection static, then you can check if you already established a connection. Static variables are the same across all instances of the class, so you can create 100 instances that all use the same connection. You just need to reference it statically: self::$dbh instead of $this->dbh.

class DB {
    private static $dbh = null;

    function __construct() {
        if ( is_null(self::$dbh) ) {
            self::$dbh = // PDO connection here
        }
    }
 }

Answer by Starx

I would suggest you to check the $this -> db at first and then only create it.

function __construct() {
        if(!isset($this -> db) || !is_a("DB", $this -> db)) {
         $this->db = new DB();    // Makes a new connection in DB::__construct()
    }
}
April 29, 2011

how to use a photoshop login in asp.net web page?

Question by Ani

I have designed a log-in page in photoshop, saved the image in png format. but I dont know how to use this image in asp.net web page. Please help in this concern or show me the resource or the way I can design a good log-in without compromising on polished look and functionality.

Thanks

Answer by Starx

Changing a PSD Template into a CSS Template is not a easy task as it seems. Comprises may steps.

You have to following steps.

  • Slice the images in your psd template.
  • Export the images
  • Try to minimize the use of photoshop generated slices as less as possible. You should try to recreate the effect using CSS the best you can.
  • You can first try this into Table Structure, which is the default way Photoshop exports the slices and the HTML.
  • Later on, create a table less layout. It should be pretty easy once you have created the table layout.

I am assuming, you know how to find the tutorials, regarding the steps I have mentioned above.

November 17, 2010

Browser/OS support fonts

Question by waanders

Where can I find a list of browser safe fonts? I mean fonts I can use in my webpages which will be displayed correctly in different browsers and versions both with Windows and Mac.

UPDATE:

Yes, I know how to search with Google. I just thought someone at this forum could give me a link to a matrix with font/browser/browserversion/os support of fonts. Like http://www.quirksmode.org/compatibility.html for CSS support.

I found something that comes close to what I meant:
http://www.upsdell.com/BrowserNews/res_fonts.htm

Answer by Oded

There are such lists:

These is just one good link from a quick google search for “safe web font”.

You can learn quite a lot from the wikipedia entry for Web Typography.

Answer by Starx

Go for most common fonts between all domains. I would suggest Verdana, it is probably one of the widely used font, which is supported by almost all OS.

Or go from one of the following list

font-family: Arial, Helvetica, sans-serif;
font-family: 'Arial Black', Gadget, sans-serif;
font-family: 'Bookman Old Style', serif;
font-family: 'Comic Sans MS', cursive;
font-family: Courier, monospace;
font-family: 'Courier New', Courier, monospace;
font-family: Garamond, serif;
font-family: Georgia, serif;
font-family: Impact, Charcoal, sans-serif;
font-family: 'Lucida Console', Monaco, monospace;
font-family: 'Lucida Sans Unicode', 'Lucida Grande', sans-serif;
font-family: 'MS Sans Serif', Geneva, sans-serif;
font-family: 'MS Serif', 'New York', sans-serif;
font-family: 'Palatino Linotype', 'Book Antiqua', Palatino, serif;
font-family: Symbol, sans-serif;
font-family: Tahoma, Geneva, sans-serif;
font-family: 'Times New Roman', Times, serif;
font-family: 'Trebuchet MS', Helvetica, sans-serif;
font-family: Verdana, Geneva, sans-serif;
font-family: Webdings, sans-serif;
font-family: Wingdings, 'Zapf Dingbats', sans-serif;
May 20, 2010

what's the best technology for creating a web site which manages users accounts?

Question by embedded

I’m going to create a web site which should manage users accounts.
A user opens an account and gets relevant data for his needs.

What technology should I be using to create this site?
Are there any templates I could use?

I don’t have the time to take care of the web site’s desgin,
Are there any places where I can buy some graphics?

Thanks

Answer by Starx

Well, if you are going for rapid application development, i suggest using a framework or a CMS as suggested by @Glycerine.

But I am not understanding what you mean by technology? I am not sure that there is anything called “technology” in php.

Apache is a server technology I am familiar with.

...

Please fill the form - I will response as fast as I can!