VOOZH about

URL: https://rubychallenger.blogspot.com/search/label/php

⇱ The Ruby Challenger: php


Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Sunday, July 24, 2016

Slides: Introduction to Ruby

I'm here today to share a presentation that I've made to a group of Software Development students.

Click here to see the presentation.

I'm sorry for a piece of code that was not translated (variable names in portuguese), but I think that you can get the idea (comparing how PHP is spelling and how Ruby is expressive).

Tuesday, September 24, 2013

How to change the default mode for php files in Kate Editor

Whenever I open a PHP file within the excellent Kate Editor (which I prefer due to its multi-document interface), the default mode for editing the file is PHP (HTML) instead of PHP/PHP. I use much more the indentation of the last one. I always can change the mode thru Tools -> Mode -> Scripts -> PHP/PHP, but it's a pain to do it every time I open a PHP file.

But you can set the default mode to PHP/PHP by changing its priority compared to PHP (HTML)'s.

That's how I did it:

  • Go to Settings -> Configure Kate -> Open/Save -> Modes & Filetypes;
  • Select Scripts/PHP (HTML) Filetype;
  • Change Priority to 4;
  • Select the File Extensions field and copy it;
  • Select Scripts/PHP/PHP Filetype;
  • Go to the File Extensions and paste the content copied;
  • Hit Apply and Ok;
  • Close and reopen Kate.


I was hard to find that solution (see reference); I hope this can be useful.

Thursday, March 24, 2011

Ruby autoload like in PHP

(Disclaimer: this is a translation from Autoload do Ruby como no PHP, in Portuguese.)

Who has worked with OOP in PHP and starts to learn Ruby soon will miss the PHP __autoload.

In fact, Ruby has autoload, but it seems useless for a PHP programmer.

In Ruby, the autoload is for using less memory, leaving files to be loaded only when necessary. But then you need to type more than a simple require; besides specifying the filename, you need also type the constants (classnames) which are in the file. For example:

autoload :MyClass, "myclass.rb"

I guess the computers was made to help the people, and that includes the programmer. From the programmer point of view (and not from the machine's one), would be far easier to type:

require "myclass"

Unless resources are critical, the development/maintenance time is more important in this case.

In PHP, you define only once a function which will guess the filename from the classname, and then you don't worry ever more. You don't need to specify nothing!!! When a reference to "MyClass" appears, the function __autoload will convert it to a filename (e.g., "myclass.inc"; you can code __autoload with your needs) and will try to include that file. That's awesome useful!

Fortunately, Ruby is a very powerful language, and, following a tip, I made my version:

# autoload like in PHP
# (c) Sony Santos 2011
# License: Public Domain
# http://rubychallenger.blogspot.com/2011/03/ruby-autoload-like-in-php.html

# alters Object#const_missing
#
class Object

 # it's a class method, not an instance one
 class << self

 # preserves the original method
 alias_method :old_const_missing, :const_missing
 private :old_const_missing

 # redefine the method which is called on a missing const
 def const_missing(const)

 # guess a filename (use your favorite algorithm)
 filename = const.to_s.downcase

 begin
 # try to require it
 require filename

 # if it did, returns that missing const
 const_get(const)

 rescue LoadError => e
 if e.message.include? filename

 # that constant doesn't really exist; use the old method
 old_const_missing(const)

 else

 # other reason
 raise

 end
 end
 end
 end
end
Subscribe to: Posts (Atom)