免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2477 | 回复: 1
打印 上一主题 下一主题

PHP 和 Ruby 的基本常量变量、类的简单书写 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-12-18 17:27 |只看该作者 |倒序浏览
PHP 和 Ruby 的基本常量变量、类的简单书写











PHP 变量、常量
变量:$var = "1000";
常量:define("ROOT","/tmp");
类常量:const aaa = 10;
类变量:同样还是$xxx;
全局变量: global $xxx; $GLOBALS['xxxx']

Ruby 变量、常量
变量:aa = 10 或者 _a = 10 小写字母或者下划线开头
常量:Ma = 10
实例变量:@hello = 10
类变量:@@t = 20
全局变量:$aa = 10

PHP基本的类实例化
Php代码
  1. <?php   
  2. class demo {   
  3.     function __construct($name) {   
  4.         $this -> name = $name;   
  5.     }   
  6.     function say() {   
  7.         echo $this -> name;   
  8.     }   
  9.     function __destruct() {   
  10.     }   
  11. }   
  12. $p = new demo("yang");   
  13. $p -> say();  

  14. <?php
  15. class demo {
  16.     function __construct($name) {
  17.         $this -> name = $name;
  18.     }
  19.     function say() {
  20.         echo $this -> name;
  21.     }
  22.     function __destruct() {
  23.     }
  24. }
  25. $p = new demo("yang");
  26. $p -> say();
复制代码
Ruby基本的类实例化
Ruby代码
  1. class Hello   
  2.     def initialize( name )   
  3.         @name = name   
  4.     end  
  5.     # php function __construct   
  6.   
  7.     def hello_rb   
  8.         puts "hello"+@name  
  9.     end  
复制代码
end
  1. hi = Hello.new("phper.yang")   
  2. hi.hello_rb  

  3. class Hello
  4.     def initialize( name )
  5.         @name = name
  6.     end
  7.     # php function __construct

  8.     def hello_rb
  9.         puts "hello"+@name
  10.     end
复制代码
end
  1. hi = Hello.new("phper.yang")
  2. hi.hello_rb
复制代码
PHP类的简单继承
Php代码
  1. class my {   
  2.     function say() {   
  3.         echo "hello ";   
  4.     }   
  5. }   
  6.   
  7. class hhy extends my {   
  8.     function yang() {   
  9.         echo "yang";   
  10.     }   
  11. }   
  12. $p = new hhy();   
  13. $p -> say();   
  14. $p -> yang();  

  15. class my {
  16.     function say() {
  17.         echo "hello ";
  18.     }
  19. }

  20. class hhy extends my {
  21.     function yang() {
  22.         echo "yang";
  23.     }
  24. }
  25. $p = new hhy();
  26. $p -> say();
  27. $p -> yang();
复制代码
Ruby类的简单继承
Ruby代码
  1. class Hello   
  2.   def t1   
  3.      yang = "hello ruby"  
  4.      puts yang   
  5.   end  
  6. end  
复制代码
  1. class Newhello < Hello   
  2.    def t2   
  3.       yphp = "hello php"  
  4.       puts yphp   
  5.    end  
  6. end  
  7.   
  8. p = Newhello.new  
  9. p.t1   
  10. p.t2  

  11. class Hello
  12.   def t1
  13.      yang = "hello ruby"
  14.      puts yang
  15.   end
  16. end

  17. class Newhello < Hello
  18.    def t2
  19.       yphp = "hello php"
  20.       puts yphp
  21.    end
  22. end

  23. p = Newhello.new
  24. p.t1
  25. p.t2
复制代码
ruby 的类名首字母必须大写

论坛徽章:
0
2 [报告]
发表于 2011-12-19 11:25 |只看该作者
学习了..谢谢分享了
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP