PHP 和 Ruby 的基本常量变量、类的简单书写

本文详细介绍了PHP与Ruby中变量、常量、类实例化的基本概念,包括变量类型(普通变量、全局变量)、常量使用、类变量与实例变量的定义与应用,并通过实例展示了基本的类实例化与继承过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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
class demo {
function __construct($name) {
$this -> name = $name;
}
function say() {
echo $this -> name;
}
function __destruct() {
}
}
$p = new demo("yang");
$p -> say();


Ruby基本的类实例化

class Hello
def initialize( name )
@name = name
end
# php function __construct

def hello_rb
puts "hello"+@name
end

end
hi = Hello.new("phper.yang")
hi.hello_rb


PHP类的简单继承

class my {
function say() {
echo "hello ";
}
}

class hhy extends my {
function yang() {
echo "yang";
}
}
$p = new hhy();
$p -> say();
$p -> yang();


Ruby类的简单继承

class Hello
def t1
yang = "hello ruby"
puts yang
end
end

class Newhello < Hello
def t2
yphp = "hello php"
puts yphp
end
end

p = Newhello.new
p.t1
p.t2

ruby 的类名首字母必须大写
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值