Jump to content

deathbeam

Members
  • Posts

    22
  • Joined

  • Last visited

Posts posted by deathbeam

  1. So, I am not very experienced with regexes, so I wanna ask if this code can be simplified with preg_match? It is code from my framework on what I am working for adding routes. To explain what is "default" and "MAP", here is example usage. And, callable is called via call_user_func_array if that helps.

    // mapping routes
    $fw->route('home: GET|POST /', 'home');
    
    // provide ReST interface by mapping HTTP requests to class method
    $fw->route('MAP /rest', 'some_class');
    
    // default route (404 page)
    $fw->route('default', 'error');
    

    And this is route method.

    public function route($pattern, $callable) {
    		$pattern = strtr($pattern,array(' '=>''));
    		
    		if ($pattern == 'default') {
    			$this->default_route = $callable;
    			return $this;
    		}
    
    		$arr = explode('/', $pattern, 2);
    		$method = $arr[0];
    		$route = '/'.$arr[1];
    		
    		if (strpos($arr[0], ':') !== false) {
    			$arr = explode(':', $arr[0], 2);
    			$name = $arr[0];
    			$method = $arr[1];
    		}
    		
    		if ($method == 'MAP') {
    			foreach ((explode('|', self::Methods)) as $method) {
    				$this->route((isset($name)?$name.':':null).$method.$route, $callable.'->'.strtolower($method));
    			}
    			return $this;
    		}
    		
    		$this->routes[] = array($method, $route, $callable, isset($name)?$name:null);
    		return $this;
    	}
    
  2. Most modern framework use autoloaders to including classes

     

    Also see

    http://php.net/manual/en/function.spl-autoload-register.php

    http://www.php-fig.org/

     

    I was not asking about using autoloader, that will defeat purpose of simplifying my code, why should I use autoloader when my framework is only single php file?

     

    also, putting php settings into a .htaccess file only works when php is running as an apache module, which is uncommon on shared web hosting. so doing it the way you are trying would limit your framework to just apache servers and to just a fraction of those that have php running as a server module.

     

    Oh, thank you very much for this info. I am focusing on ability to use my fw on shared hosting, so then I will scrap this idea.

  3. There is also another scenario. Like this:

    class base{
         public function setParam($key, $callable){
              $this->params[$key] = $callable;
         }
         internal function run(){
              foreach($this->params as $key=>$callable)
                     $callable();
         }
    }
    $base = new Base();
    // if internal was possible, users will not be able to call this method outside this file. Especially usefull for frameworks and PHP applications.
    $base->run();
    

    If you know what I mean. And yes, PHP isn´t C#, but PHP hosting is cheaper than ASP.NET. So it is pity that internal does not exists in PHP.

  4.  

    Not sure what your talking about but php does have access modifiers

    class phpfreak{
         protected age;
         protected height;
    
         protected function getage(){
              return $this->name;
         }
         protected function getheight(){
              return $this->height;
         }
    }
    
    class create_freak extends phpfreak{
         public function create_freak($age, $height){
              $this->age = 20;//protected
              $this->height = 100;//Protected
         }
         public function get(){
              return Array(parent::getage(), parent::getheight());
         }
    }
    
    $create_a_freak = new create_freak();//calls constructor
    print_r($create_a_freak.get()); //gets values
    

     

    Yes, it have protected, private and public. But it misses internal. Internal functions and vars are visible only in file.

  5. So I am working on my own PHP framework and I am using this:

    php_value auto_prepend_file library/fw.php
    php_value auto_append_file library/fw.php
    

    To auto prepend and append file. In that file I have check if instance of core class located in fw.php is defined so it will not prepend and append duplicates. With this I reduced index.php code by these 2 lines

    // This should be at beggining of index php
    $fw = Base::getInstance();
    
    // This should be at very end of index.php
    $fw->run();
    

    Is it good or bad way?

  6. Only thing I really miss from PHP is "internal" access modifier. Before I was making libraries and extensions in C# (like game frameworks, Tiled implementations, GUI loaders). Now, when I am working on my own PHP framework, I really miss internal, I was using it a lot in C#. Is internal planned to be added in PHP 7 or is it already in PHP 5.6 or it will never be added?

  7. If you are echoing PHP values to html and you need to rewrite all relative links then yes, this is probably best method. Otherwise use simple string merging and echo values with var like $newURL. before them

  8. It will change to what url will point relative urls in your document. F.E when your domain is www.example.com, this relative path

    <a href="test">Test relative link</a>
    

    will point to www.example.com/test. But if you will change base to f.e this one

    <base href="http://www.new_example.com/">
    

    "Test relative link" will point to www.new_example.com/test

  9. thanks for the feedback? I actually like the design, but that is not the reason for my post. When you have scrolled to the "About Me" section Resume is highlighted in the menu, and when you are in the "Resume" section Portfolio is highlighted in the menu.

     

    I am using Chrome.

     

    I am using chrome too, and when I scroll to About Me, About Me is highlighted and same is with Resume. On my PC your site is working perfectly.

  10. So I was working on this site past month. I know it is too much time for such simple website, but I am a perfectionist and I need to have everything perfect. It is using JQuery, Twitter Bootstrap (I am deep fan of TWBS and I am using it on almost every website I make) and Yeti(?probably, I forgot :D) theme from Bootswatch. Of course also FontAwesome for YT, FB, Twitter an GitHub icons. On smaller resolutions it is looking epic (1024x768 etc etc), on mobile devices too, but I am not sure how it looks on HD, HD-ready resolutions (yes, I can use zoom-out in my browser, but it is misleading...). What do you think?

     

    P.S.: Projects in "My Recent Work" are clickable and that will toggle description with some fancy fade effect (using jQuery.toggle()).

     

    P.S.2: I am not sure if I should vertically-center contact text or let it be with big top and bottom padding. What do you think?

     

    P.S.3: Oh, I almost forgot. Website link is here http://deathbeam.github.io/. Yes, I am using GitHub pages, I love git. And nope, I do not bought my own domain yet.

     

    P.S.4: I accept "brutal" criticizm, feel free to be rude :D

     

    P.S.5: (this is starting to be annoying lol): If you guys have time, can you please tell me what feeling do you have from my subsite http://deathbeam.github.io/fwphp/index.htm. I tried to keep everything as simple as possible, but I am not sure if it is not too much :/

     

    P.S.6: I am really sorry for my English...

  11. I am using something like this for storing files

    if($_SERVER['REQUEST_METHOD'] == 'POST') {
      preg_match('/\.([a-zA-Z]+?)$/', $_FILES['userfile']['name'], $matches);
      if(in_array(strtolower($matches[1]), $accepted)) {
        $newname = md5_file($_FILES['userfile']['tmp_name']).'.'.$matches[1];
        move_uploaded_file($_FILES['userfile']['tmp_name'], $filedir.'/'.$newname);
      }
    }
    

    Where '$accepted' is array of allowed file extensions and '$filedir' is subdirectory to store images. I am using 'md5_file' for unique names for images. Then you can simply pass '$newname' to database what is containing other data such as uploader and description like you stated in OP.

  12. No, no, you don't understand. Template inheritance means that I can create a base template and then override specific parts of it in subtemplates – similar to inheritance in OOP. This is incredibly useful and leads to very clean templates.

     

    This can be done with closures.

     

    Why is learning new tools such a big problem? A developer should be able to learn and use all kinds of tools, especially in web programming. If they can't, then, frankly, this job is not for them.

     

    You're worried about performance? Do you have concrete figures at hand, or is this just a vague feeling? All modern engines cache the templates as plain PHP scripts, which means there's no technical difference between a handwritten PHP “template” and a Twig template after the initial parsing stage. I very much doubt that there's any significant performance loss. But feel free to prove your point.

     

    It isn´t problem and I was using both Smarty and Twig. But I am not fan of them. Why? Becouse IMO it is overkill. I never liked using more code than I need, and using XXXKBs more of code just to have comfortable way to differ presentation from bussiness logic is not my style. PHP short tags and alternative tags like endif, endfor etc are enought readable for me. And yes, modern templating engines are caching their templates, but they still need to cache them at first load (and re-cache after every modification) and that takes some more miliseconds (yes, it isn´t noticeable, but it IS performance lost).

     

     

     

    And that tells us what exactly? That you can throw together some lame tag replacer in 2 minutes? No doubt about that. But I'm talking about a professional template engine.

     

    Okay, I agree and this is valuable point for me (yes, it is still my own opinion, I know that others can have different opinions on this). Again, I will repeat what I said before. I do not like using more code than I need. And using professional template engine only becouse it have great auto-escaping feature....

     

    How do you know what I need?

    As I said, this is my subjective opinion, my 2 cents. It is feature without what I can continue living.

  13. So I am just wondering to what music/genres you guys are listening too when working on something. This is somewhat forum game, so I will start. When I am programming something, I am mostly listening to metal (almost all genres of metal, including nu-metal and metalcore). Right now I am listening to Sabaton - Primo Victoria

  14.  template engine !== new language. 

     

    You can learn in just two days how to use Twig or Blade.

     That´s why I put language into " ". It is something like that, another thing to learn when you do not have to.

     

    Until fairly recently I felt the same, however, you should read the points brought up by Jacques1 again. In particular, I can't go past: "PHP is a template engine from the 90s. It doesn't have any modern features whatsoever: no template inheritance, no auto-escaping, no custom tags."

     

    Template inheritance can be done with "include". Auto-escaping can be done with simple preg_replace when using object buffering to get PHP file. Here is stripped down example what I am doing:

    public function draw($file) {
      extract($this->fields);
      ob_start();
      include $file;
      $html = ob_get_clean();
      echo preg_replace("insert auto","escaping here", $html);
    } 

    And custom tags? Not needed really. But I agree that Jacques is right in most of his points, but properly used PHP as template engine is a lot better than using 3rd party or custom-built template engines in terms of speed and without longer learning curve.

  15. IMO better is use PHP as templating language, becouse PHP IS templating language. Using template engines like Smarty, Twig, Blade or Moustache is imo only slowing your code.

     

     

    I use Blade template engine. I like {{$var}} more than <?=$var?>

     

    One example:

    @if(true)
       asdsdasd
    @endif

    is more readable than

    <?php if(true): ?>
       asdasda
    <?php endif; ?>

     

    It is similar enought. Why people should learn new "language" only for writing templates? HTML, CSS, JS and PHP is enought.

  16. Here is basic HTML5 layout for you:
     

    <!-- PHP code here -->
    <!doctype html>
        <head>
            <meta charset="utf-8">
            <title>The Default Template</title>
            <!-- CSS, favicons etc goes here -->
        </head>
        <body>
            <header>
                
            </header>
            <section>
                
            </section>
            <footer>
                
            </footer>
        <!-- Place your javascript here -->
        </body>
    </html>
    
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.